ballouta Posted May 20, 2009 Share Posted May 20, 2009 Hello I have this working validatiuon script. <script type="text/javascript" language="Javascript" > <!-- function verification() { if(document.registration.username.value == "") { alert("Name required"); document.registration.username.focus(); return false; } else if(document.registration.email.value == "") { alert("Email required"); document.registration.email.focus(); return false; } else if(document.registration.email.value.indexOf('@') == -1) { alert("email not valid"); document.registration.email.focus(); return false; } else if(document.registration.phone.value == "") { alert("Phone number is empty!"); document.registration.phone.focus(); return false; } else if(document.registration.country.value == "") { alert("Country is empty!"); document.registration.country.focus(); return false; } return true } //--> </script> i want to add more validation for the username (it should be between 4 and 12 characters) is there better validation for the email address? many thanks Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/ Share on other sites More sharing options...
fanfavorite Posted May 22, 2009 Share Posted May 22, 2009 Try these: username: username = document.registration.username; if(username.value == "" OR username.length < 4 OR username.length > 12) { email: pattern = /^([a-zA-Z0-9_-])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9]+)+$/; if( !pattern.test( document.registration.email.value ) ) { Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839611 Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 fanfavorite, I think you're thinking about PHP. JavaScript doesn't work with the keyword OR in a conditional. Also, your condition is wrong. Please check. Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839637 Share on other sites More sharing options...
fanfavorite Posted May 22, 2009 Share Posted May 22, 2009 What do you mean doesn't work with the keyboard? Those functions work for me in javascript. if(username.value == "" || username.length < 4 || username.length > 12) { Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839643 Share on other sites More sharing options...
fanfavorite Posted May 22, 2009 Share Posted May 22, 2009 Wow I am tired lol. Just read your post again and realized you said keyword "OR" lol. The condition is right. If the value is null, is less than 4 or greater than 12, then it is invalid. Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839644 Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 Oh really? username.length > 4 really works? Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839648 Share on other sites More sharing options...
fanfavorite Posted May 22, 2009 Share Posted May 22, 2009 I haven't tested this posted code, but I have done things similar. At second look, I think it needs to be username.value.length Been looking at code all day, need to get some shuteye. Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839670 Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 Yup, that was it. Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-839687 Share on other sites More sharing options...
ballouta Posted May 23, 2009 Author Share Posted May 23, 2009 Thanks I just added .value before .length and it is working Link to comment https://forums.phpfreaks.com/topic/158894-solved-script-modification/#findComment-840593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.