I’ve got the code below, in my page for a form, but it just checks to see if a field is blank or not. Is there an easy and quick way to check for the proper email format as well please ? Not looking for anything more complicated than that, just the correct email format, not whether the email address exists for example.
Any help very much appreciated.
Dez.
function validate() {
if (document.email.value.length==0) {
alert(“please enter your email address”);
return false;
}
if (document.form.name.value.length==0) {
alert(“please enter your name”);
return false;
}
Many thanks for that, it is appreciated. I did Google it, but must have missed that one.
One thing, I’m new to the email validation thing - is the code that I’m using Javascript ? If so, does anyone know of an easy, quick way to do the same thing, but without Javascript please ?
var EmailRegExp = new
RegExp(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
z0-9]|(-[A-Za-z0-9]))+)\\.([A-Za-z]{2,6})(\\.([A-Za-z]{2}))?$/);
Yes, your code is JavaScript. What other language would you want to use instead (there aren’t any alternatives if you want to do the validation client-side)?
With PHP, the form would have to be submitted to the server, validated, then if there were errors it would then be returned to the original page and present the user with the details of their error. It’s more work than the JavaScript-only version (although you should really have both).
If you had php on it, why would you also need js as well ? I thought php would do it all ?
Also, how would I combine the code below :
function validate() {
if (document.email.value.length==0) {
alert(“please enter your email address”);
return false;
}
if (document.form.name.value.length==0) {
alert(“please enter your name”);
return false;
}
document.form.submit()
return true;
}
With the code below please :
Dez.
var EmailRegExp = new
RegExp(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
z0-9]|(-[A-Za-z0-9]))+)\\.([A-Za-z]{2,6})(\\.([A-Za-z]{2}))?$/);
Because it would be nice to your users if they didn’t have to wait for the page to refresh before they found out they made a mistake.
Also, how would I combine the code below
function validate() {
if (document.email.value.length==0) {
alert("please enter your email address");
return false;
}
if (document.form.name.value.length==0) {
alert("please enter your name");
return false;
}
if (!document.form.name.value.match(/^[A-Za-z0-9]([A-Za-z0-9_-]|(\\.[A-Za-z0-9]))+@[A-Za-z0-9](([A-Za-
z0-9]|(-[A-Za-z0-9]))+)\\.([A-Za-z]{2,6})(\\.([A-Za-z]{2}))?$/) {
alert("please enter a valid email address");
return false;
}
document.form.submit()
return true;
}
You should also not use JavaScript to submit the form.
It’s nigh on impossible to filter valid e-mails with regexp, especially now when non-ascii characters are allowed. At least åäö and ’ are valid in email addresses.
If you don’t want to filter out any valid email addresses by mistake then the safest bet it to just assume that there will be an @ somewhere in it, so