ballouta Posted June 23, 2008 Share Posted June 23, 2008 Hello I googled for a function that validates if the username or password fields submitted are between 5 and 12 characters but my form was damaged. I give up. Please Help. This is the .js file that I have and it working for the Full Name (fname), and email. I wanna make sure that the subscriber is writing characters between 4 and 12. would you please give me a fucntion that i can include in my js file ? function isReady() { // check if the First Name is valid if (isProper(frm.fname.value) == false) { alert("Please enter your First Name"); return false; } // check if the email address is valid if (isEmail(frm.email.value) == false) { alert("Please enter a valid email address"); return false; } // check if the country is selected if (isProper(frm.country.value) == false) { alert("Please select a country"); return false; } // check if the User Name is valid if (isProper(frm.user.value) == false) { alert("Please enter a valid username"); return false; } // check if the Password is valid if (isProper(frm.pass.value) == false) { alert("Please enter a correct password"); return false; } return true; /**********************************************/ } function isEmail(string) { if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true; else return false; if (!string) return false; var iChars = "*|,\":<>[]{}`\';()&$#%"; for (var i = 0; i < string.length; i++) { if (iChars.indexOf(string.charAt(i)) != -1) return false; } return true; } /**********************************************/ function isProper(string) { if (string.search(/^\w+( \w+)?$/) != -1) return true; else return false; if (!string) return false; var iChars = "*|,\":<>[]{}`\';()@&$#%"; for (var i = 0; i < string.length; i++) { if (iChars.indexOf(string.charAt(i)) != -1) return false; } return true; } Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 23, 2008 Share Posted June 23, 2008 You can use a function like this: <script language="Javascript"> function validate(text) { if (text.length > 3 && text.length < 13) return true; else return false; } </script> You can either change the return code to do what you want it to do or call it like: if (validate(textbox1.value)) alert("valid"); Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2008 Share Posted June 23, 2008 BY the way, I believe there is a problem with the isProper function you posted above: function isProper(string) { if (string.search(/^\w+( \w+)?$/) != -1) return true; else return false; if (!string) return false; var iChars = "*|,\":<>[]{}`\';()@&$#%"; for (var i = 0; i < string.length; i++) { if (iChars.indexOf(string.charAt(i)) != -1) return false; } return true; } The first IF/ELSE statemetn will either return true or false, so everything after that is never run. This should fix it if I am reading it correctly: function isProper(string) { if (string.search(/^\w+( \w+)?$/) == -1) return false; if (!string) return false; var iChars = "*|,\":<>[]{}`\';()@&$#%"; for (var i = 0; i < string.length; i++) { if (iChars.indexOf(string.charAt(i)) != -1) return false; } return true; } Also, you could replace the for loop with a single regex pattern. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.