Hello,
I have a form which requires the fields to be valid before the buttons become active. In particular I am having a problem with the email address field.
The form is written in PHP using a print function but thats not relevant to my problem. (I am just telling you so it makes it easier to understand why I have used escape chars around the quote marks)
The e-mail address field is giving me all kinds of problems, in firefox and safari and camino when i enter my email address it gives me the alert saying it is valid but when in IE6 & IE7 it is saying its not valid even though, char for char it is an identical one to that which is valid in FF and is actually a valid address.
The function testing is triggered on the onkeyup action.
The email test function is...
[CODE=javascript]function mailTest(){
//The string to test.
var n = document.getEle mentById("formM ail").value.toS tring();
//How long it is
var l = n.length;
//Getting the position of the @ symbol
var at = n.indexOf("@");
//Gettng the position of the first dot
var dot = n.indexOf(".");
//Array of illegal charachters that should not be in Email addresses
var badChars = new Array("§","±"," !","¡","€","#", "£","$","%","^" ,"&","*","(",") ","+","="," {","[","}","]",";",":","'"," \"","\\","|","< ",",",">"," ?","/"," ");
//A variable counting the number of illegal chars, default value is 0
var badCount = 0;
//For loop, cycling through the array of illegal characters.
for(var i=0; i < badChars.length ; i++){
//If the tested illegal char is in the address string
if(n.indexOf(ba dChars[i]) != -1){
//Add 1 to the badChar counter
badCount++;
}
}
//Get the number of @ symbols in the mail, should only be 1
var atCount = stringCounter(" @", n);
//Check the number of . symbols, must be at least 1, have yet to encounter a mail address with more than 4
var dotCount = stringCounter(" .", n);
//Check the position of the last . in the address should be after the @ but not immediately, and should be at least 2 chars before the end.
var lastDot = n.lastIndexOf(" .");
//Testing if the address is null, or less than 8 chars, where the @ and . symbols are and if it contains any illegal chars
if(n != "" && l >= 8 && at != -1 && at != 0 && at < l-4 && dot != -1 && dot !=0 && dot != at+1 && dot != at-1 && lastDot >= l-5 && lastDot > at+2 && lastDot < l-2 && badCount == 0 && atCount == 1 && dotCount >= 1 && dotCount <= 4){
//If all checks pass then it is a valid email address
mailPass = true;
alert("email valid");
}else{
//If any of the tests fail then the address is not valid.
mailPass = false;
alert("email not valid");
}
}
function stringCounter(c harToCount, stringToSearch) {
//The char we wish to count
var needle = charToCount;
//The string we are counting in
var heystack = stringToSearch;
//The counter saying how many times the char has occured in the string.
var counter = 0;
var myArray = heystack.toLowe rCase();
for (i=0;i<myArray. length;i++){
if (myArray[i] == needle){
counter++;
}
}
return counter;
}[/CODE]
I know it is not a perfect syntax checker, yet, but it should work, and does in non IE browsers.
The HTML which calls the function is...
[PHP]print("<tr><td> Email Address</td><td><input onkeyup=\"mailT est()\" id=\"formMail\" type=\"text\" name=\"emailAdd \" style=\"width:2 00px;\" />*</td></tr>");[/PHP]
Any insight is greatly appreciated, kind regards, Michael.
I have a form which requires the fields to be valid before the buttons become active. In particular I am having a problem with the email address field.
The form is written in PHP using a print function but thats not relevant to my problem. (I am just telling you so it makes it easier to understand why I have used escape chars around the quote marks)
The e-mail address field is giving me all kinds of problems, in firefox and safari and camino when i enter my email address it gives me the alert saying it is valid but when in IE6 & IE7 it is saying its not valid even though, char for char it is an identical one to that which is valid in FF and is actually a valid address.
The function testing is triggered on the onkeyup action.
The email test function is...
[CODE=javascript]function mailTest(){
//The string to test.
var n = document.getEle mentById("formM ail").value.toS tring();
//How long it is
var l = n.length;
//Getting the position of the @ symbol
var at = n.indexOf("@");
//Gettng the position of the first dot
var dot = n.indexOf(".");
//Array of illegal charachters that should not be in Email addresses
var badChars = new Array("§","±"," !","¡","€","#", "£","$","%","^" ,"&","*","(",") ","+","="," {","[","}","]",";",":","'"," \"","\\","|","< ",",",">"," ?","/"," ");
//A variable counting the number of illegal chars, default value is 0
var badCount = 0;
//For loop, cycling through the array of illegal characters.
for(var i=0; i < badChars.length ; i++){
//If the tested illegal char is in the address string
if(n.indexOf(ba dChars[i]) != -1){
//Add 1 to the badChar counter
badCount++;
}
}
//Get the number of @ symbols in the mail, should only be 1
var atCount = stringCounter(" @", n);
//Check the number of . symbols, must be at least 1, have yet to encounter a mail address with more than 4
var dotCount = stringCounter(" .", n);
//Check the position of the last . in the address should be after the @ but not immediately, and should be at least 2 chars before the end.
var lastDot = n.lastIndexOf(" .");
//Testing if the address is null, or less than 8 chars, where the @ and . symbols are and if it contains any illegal chars
if(n != "" && l >= 8 && at != -1 && at != 0 && at < l-4 && dot != -1 && dot !=0 && dot != at+1 && dot != at-1 && lastDot >= l-5 && lastDot > at+2 && lastDot < l-2 && badCount == 0 && atCount == 1 && dotCount >= 1 && dotCount <= 4){
//If all checks pass then it is a valid email address
mailPass = true;
alert("email valid");
}else{
//If any of the tests fail then the address is not valid.
mailPass = false;
alert("email not valid");
}
}
function stringCounter(c harToCount, stringToSearch) {
//The char we wish to count
var needle = charToCount;
//The string we are counting in
var heystack = stringToSearch;
//The counter saying how many times the char has occured in the string.
var counter = 0;
var myArray = heystack.toLowe rCase();
for (i=0;i<myArray. length;i++){
if (myArray[i] == needle){
counter++;
}
}
return counter;
}[/CODE]
I know it is not a perfect syntax checker, yet, but it should work, and does in non IE browsers.
The HTML which calls the function is...
[PHP]print("<tr><td> Email Address</td><td><input onkeyup=\"mailT est()\" id=\"formMail\" type=\"text\" name=\"emailAdd \" style=\"width:2 00px;\" />*</td></tr>");[/PHP]
Any insight is greatly appreciated, kind regards, Michael.
Comment