I have to make an email validation that an Email address must start with a string followed by '@', followed by another string with Javascript.
Can someone show me how please?
I used this code but it gives me an error if I use a dot sign only not for every single character.
var email=document.forms["subscriptionForm"]["email"].value;
var atCharacter = email.indexOf("@");
var dotCharacterAfterAt = email.indexOf('.', atCharacter + 1);
var dotLastCharacter=email.indexOf('.', atCharacter - 1);
var dotPosition=email.lastIndexOf(".");
if (atCharacter < 1 || dotPosition < atCharacter+2 || dotPosition + 2 >= email.length) {/* I got this code from http://www.w3schools.com/js/js_form_validation.asp */
alert("Incorrect E-Mail address");
return false;
}
if (email[0] == '.'){
alert("You can't enter a dot character as a first character")
return false
}
if (email==null || email=="") {
alert("E-Mail must be filled out !");
return false;
}
if(atCharacter + 1 == dotCharacterAfterAt){
alert("You must enter a string after the @ sign");
return false;
}
if (atCharacter - 1 == dotLastCharacter){
alert("You must not enter a string before the @ sign in your address");
return false;
}
if (dotPosition == email.length - 1) {
alert("You must not enter a dot as a last character in your address");/* an error will occur and tells you that you must not enter a "." character as a last character in your address*/
return false;
}