Ok... so this code works fine in Firefox and on a PC, but in Safari and IE5 on OSX, it doesn't recognize a valid e-mail address when it's entered, so you can't send the form... it just keeps telling you that you need to enter a valid address. Any ideas on how to get around that?
Thanks in advance
function validate(form) {
rtn = '';
var err = '';
var email = form.email.value;
var fname = form.firstName.value;
var lname = form.lastName.value;
var echeck = new RegExp(/^[\w\.=-]+@[\w\.-]+\.[\w\.-]{2,4}$/);
if(fname.length == 0) {err += 'Please enter fisrt name\n';rtn = 'f';}
if(lname.length == 0) {err += 'Please enter last name\n';rtn = 'f';}
if(!echeck.test(email) || email.length == 0) {err += 'Email Address invalid\n';rtn = 'f';}
if(form.fileatt.value!=''){
extArray = new Array(".gif", ".jpg", ".bmp", ".png");
var file = document.form1.fileatt.value;
var er='';
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray == ext) {er = ''; break; }
else {er='Only image files allowed\n'}
}
if(er!=''){err += er; rtn = 'f';}
}
if(err!=''){alert(err);}
document.rtnval=(rtn=='');
}
Form Validation Problem with IE5 & Safari OSX
Moderator: General Moderators
Dont have the answer to youre problem but try this
Code: Select all
<script language="javascript">
// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
var str = elem.value;
str = str.toLowerCase( );
if (str.indexOf("@") > 1) {
var addr = str.substring(0, str.indexOf("@"));
var domain = str.substring(str.indexOf("@") + 1, str.length);
// at least one top level domain required
if (domain.indexOf(".") = = -1) {
alert("Verify the domain portion of the email address.");
return false;
}
// parse address portion first, character by character
for (var i = 0; i < addr.length; i++) {
oneChar = addr.charAt(i).charCodeAt(0);
// dot or hyphen not allowed in first position; dot in last
if ((i = = 0 && (oneChar = = 45 || oneChar = = 46)) ||
(i = = addr.length - 1 && oneChar = = 46)) {
alert("Verify the user name portion of the email address.");
return false;
}
// acceptable characters (- . _ 0-9 a-z)
if (oneChar = = 45 || oneChar = = 46 || oneChar = = 95 ||
(oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
continue;
} else {
alert("Verify the user name portion of the email address.");
return false;
}
}
for (i = 0; i < domain.length; i++) {
oneChar = domain.charAt(i).charCodeAt(0);
if ((i = = 0 && (oneChar = = 45 || oneChar = = 46)) ||
((i = = domain.length - 1 || i = = domain.length - 2) && oneChar = = 46)) {
alert("Verify the domain portion of the email address.");
return false;
}
if (oneChar = = 45 || oneChar = = 46 || oneChar = = 95 ||
(oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
continue;
} else {
alert("Verify the domain portion of the email address.");
return false;
}
}
return true;
}
alert("The email address may not be formatted correctly. Please verify.");
return false;
}
</script>
Last edited by ol4pr0 on Mon Dec 06, 2004 12:06 pm, edited 2 times in total.