Form Validation Problem with IE5 & Safari OSX

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Simonster
Forum Newbie
Posts: 2
Joined: Mon Dec 06, 2004 10:24 am

Form Validation Problem with IE5 & Safari OSX

Post by Simonster »

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=='');
}
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

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) &#123;
    var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf("@") > 1) &#123;
        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) &#123;
            alert("Verify the domain portion of the email address.");
            return false;
        &#125;
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) &#123;
            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)) &#123;
                alert("Verify the user name portion of the email address.");
                return false;
            &#125;
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar =  = 45 || oneChar =  = 46 || oneChar =  = 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) &#123;
                continue;
            &#125; else &#123;
                alert("Verify the user name portion of the email address.");
                return false;
            &#125;
        &#125;
        for (i = 0; i < domain.length; i++) &#123;
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i =  = 0 && (oneChar =  = 45 || oneChar =  = 46)) || 
                ((i =  = domain.length - 1  || i =  = domain.length - 2) && oneChar =  = 46)) &#123;
                alert("Verify the domain portion of the email address.");
                return false;
            &#125;
            if (oneChar =  = 45 || oneChar =  = 46 || oneChar =  = 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) &#123;
                continue;
            &#125; else &#123;
                alert("Verify the domain portion of the email address.");
                return false;
            &#125;
        &#125;
        return true;
    &#125;
    alert("The email address may not be formatted correctly. Please verify.");
    return false;
&#125;
</script>
Last edited by ol4pr0 on Mon Dec 06, 2004 12:06 pm, edited 2 times in total.
Simonster
Forum Newbie
Posts: 2
Joined: Mon Dec 06, 2004 10:24 am

Post by Simonster »

Thank you for a swift response, I will try and let you know.
Post Reply