Textbox validation
-
Hi all i have the below functions which i use to validate a login, albeit not very well ! this is called when the user submits the form, all it does it checked they have entered something what i need to do is to throw out an error when a user enters an alpha (a-Z) character as well but im getting a bit stuck, can ne1 point me in the right direction here ??? thanks si function isblank(s) { for(var i=0; i < s.length; i++){ var c=s.charAt(i); if((c!=' ') && (c!='\n') && (c!='\t')) return false; } return true; } function verify(f) { var msg; var empty_fields=""; var errors=""; for (var i=0; i < f.length; i++) { var e = f.elements[i]; if ((e.type == "text") || (e.type == "password")) { if ((e.value=="") || isblank(e.value)){ empty_fields += "\n "+e.name; continue; } } } if (!empty_fields && !errors) return true; msg = "___________________________________________________________\n\n" msg += "The login action could not been performed because of the following error(s).\n"; msg += "___________________________________________________________\n\n"; if(empty_fields){ msg +="The following required field(s) are empty:" + empty_fields + "\n"; if (errors) msg+="\n"; } msg+=errors; alert(msg); return false; }
-
Hi all i have the below functions which i use to validate a login, albeit not very well ! this is called when the user submits the form, all it does it checked they have entered something what i need to do is to throw out an error when a user enters an alpha (a-Z) character as well but im getting a bit stuck, can ne1 point me in the right direction here ??? thanks si function isblank(s) { for(var i=0; i < s.length; i++){ var c=s.charAt(i); if((c!=' ') && (c!='\n') && (c!='\t')) return false; } return true; } function verify(f) { var msg; var empty_fields=""; var errors=""; for (var i=0; i < f.length; i++) { var e = f.elements[i]; if ((e.type == "text") || (e.type == "password")) { if ((e.value=="") || isblank(e.value)){ empty_fields += "\n "+e.name; continue; } } } if (!empty_fields && !errors) return true; msg = "___________________________________________________________\n\n" msg += "The login action could not been performed because of the following error(s).\n"; msg += "___________________________________________________________\n\n"; if(empty_fields){ msg +="The following required field(s) are empty:" + empty_fields + "\n"; if (errors) msg+="\n"; } msg+=errors; alert(msg); return false; }
Have you looked into using Regular Expressions? Just because I don't care, doesn't mean I don't understand. - Homer J. Simpson
-
Hi all i have the below functions which i use to validate a login, albeit not very well ! this is called when the user submits the form, all it does it checked they have entered something what i need to do is to throw out an error when a user enters an alpha (a-Z) character as well but im getting a bit stuck, can ne1 point me in the right direction here ??? thanks si function isblank(s) { for(var i=0; i < s.length; i++){ var c=s.charAt(i); if((c!=' ') && (c!='\n') && (c!='\t')) return false; } return true; } function verify(f) { var msg; var empty_fields=""; var errors=""; for (var i=0; i < f.length; i++) { var e = f.elements[i]; if ((e.type == "text") || (e.type == "password")) { if ((e.value=="") || isblank(e.value)){ empty_fields += "\n "+e.name; continue; } } } if (!empty_fields && !errors) return true; msg = "___________________________________________________________\n\n" msg += "The login action could not been performed because of the following error(s).\n"; msg += "___________________________________________________________\n\n"; if(empty_fields){ msg +="The following required field(s) are empty:" + empty_fields + "\n"; if (errors) msg+="\n"; } msg+=errors; alert(msg); return false; }
use in this way, first store alpha charcater in string, and then chk character in this string. function isblank(s) { var c; var chkstring="a,b,c,d,e,f...."; for(var i=0; i < s.length; i++){ var c=s.charAt(i); if(chkstring.charAt(c) > 0) { alert("Can not enter a-z messages here"); return false; } } return true; } Himadrish Laha
-
Hi all i have the below functions which i use to validate a login, albeit not very well ! this is called when the user submits the form, all it does it checked they have entered something what i need to do is to throw out an error when a user enters an alpha (a-Z) character as well but im getting a bit stuck, can ne1 point me in the right direction here ??? thanks si function isblank(s) { for(var i=0; i < s.length; i++){ var c=s.charAt(i); if((c!=' ') && (c!='\n') && (c!='\t')) return false; } return true; } function verify(f) { var msg; var empty_fields=""; var errors=""; for (var i=0; i < f.length; i++) { var e = f.elements[i]; if ((e.type == "text") || (e.type == "password")) { if ((e.value=="") || isblank(e.value)){ empty_fields += "\n "+e.name; continue; } } } if (!empty_fields && !errors) return true; msg = "___________________________________________________________\n\n" msg += "The login action could not been performed because of the following error(s).\n"; msg += "___________________________________________________________\n\n"; if(empty_fields){ msg +="The following required field(s) are empty:" + empty_fields + "\n"; if (errors) msg+="\n"; } msg+=errors; alert(msg); return false; }
You should start by correcting your basic approach: instead of looking for errors in the input you should be testing for correct input and giving an error message if the test fails. See this article on data validation