-1

I was wondering if it were possible to force an input field to be an valid email?

<input type="text" name="emailaddress" >

I would like it to use Javascript/JQuery rather than PHP if possible!

Thanks for the help.

Night
  • 731
  • 5
  • 14
  • 2
    If you're only validating client-side, clients (people, everyday-folk) can "switch off your security". – ʰᵈˑ Jan 28 '14 at 16:31
  • validating in jquery is all fine and good, but you **ALWAYS** have to validate on the server as well... javascript validation is purely clientside and utterly trivial to bypass. – Marc B Jan 28 '14 at 16:32
  • @MarcB more importantly, you should ALWAYS confirm the email address by actually sending a message to it. – Blazemonger Jan 28 '14 at 16:39

4 Answers4

4

Support only newer browsers? Try this:

<input type="email" name="emailaddress">

Note that support is somewhat limited, but it might work for you if it falls into these browsers:

http://caniuse.com/#feat=forms

On a related note:

Validate email address in JavaScript?

Community
  • 1
  • 1
Mike Robinson
  • 24,971
  • 8
  • 61
  • 83
3
function IsEmail(email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
}
Faytraneozter
  • 865
  • 2
  • 10
  • 18
1

You can use the answer @Mike Robinson gave along with some simple validation (found here) as a fallback for older browsers:

HTML:

<input type="email" name="emailaddress" id="valid_email"><div class="validate">Please enter a valid email!</div>

JS:

$('.validate').hide();
$('body').on('blur', '#valid_email', function() {
    $('.validate').hide();
    if(!isValidEmailAddress($(this).val())) {
       $('.validate').show();
    }
});

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/i);
    return pattern.test(emailAddress);
};

Fiddle

Or, even simpler, you can just use the jQuery Validation Plugin.

You'll also want to validate on the backend, you can do this with PHP filters:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // do something if email is valid
}
Community
  • 1
  • 1
Samsquanch
  • 8,866
  • 12
  • 50
  • 89
-1

JavaScript cant know if the E-Mail is Valid. (at least in no easy way) To do this in a Dynamic Fashion, you only have the PHP Option. In Javascript you only can Use Ajax to call this with events.

EDIT: To prevent Missunderstanding

Yes you can RegEx, and somewhat with JS. But you only get a Client Side Solution on that. Also a Browser related Test is no real Solution. Validation of Inputs, is about getting the corrent and secure Input. You will only be able, to prevent Users from sending wrong Data, with a PHP framework. Or at least, you use some verry fancy (and i guess expensive) js frameworks, that may protect you.

What if I simply turn off Javascript and use an Old browser so avoid the Validation. Using HTML5 and JavaScript for Validation is only for Usabillity. Not for Security!

Panade
  • 309
  • 3
  • 12
  • You can use a regex in javascript to check if some text is formatted like an email address would be formatted. That is what the OP is asking about. – forgivenson Jan 28 '14 at 16:36
  • 1
    To be Honest, I dont know why this downratings come together. Valid Email Adress ==> Email Adress that is not only Valid in Syntax, also Valid in general. You only can Archieve this with PHP. Using Browser specific Functions, is no general solution in this case. – Panade Jan 28 '14 at 16:38
  • @forgivenson Sorry but this is too trivial. You cant give someone an advice in validation, by telling him, its alright to only check Client Side and Browser Specific stuff. Yes there is RegEx an many other Ways to check this, but this is no real Validation. In my eyes, thats always a shorthand usabillity Check. You only use js to tell the user, that its wrong. Preventing the Action, must be done in PHP. – Panade Jan 28 '14 at 16:40
  • 1
    Yes, I know you have to send an email to the address to actually verify that the email account exists, and check the address on server side, but you will still want to put javascript validation to quickly show the user if they typed an invalid email address or not. Your answer makes it sound like this is not possible to do. – forgivenson Jan 28 '14 at 16:45
  • @forgivenson Accepted the Missunderstanding Potential. I Edited this answer, to Point out the outcome of this Discussion. Its still an Important point, in this kind of Question. This is why I dont simply delete the Answer. – Panade Jan 28 '14 at 16:49