Full version: jsB@nk » Form » Validation » Forms Validation
URL: https://www.javascriptbank.com/forms-validation.html
This JavaScript can be used to check form input, not only for all required fields being completed, but can also make certain that fields requiring number (integer) or date input have been filled in correctly.
Full version: jsB@nk » Form » Validation » Forms Validation
URL: https://www.javascriptbank.com/forms-validation.html
<SCRIPT language=JavaScript><!--function check(form,x){//If you would like to use this script please do not remove the next 4 linesscript_name = "Form Validator ver 1.0"action = "Checks Required, Integer and Date"copyright = "(c) 1998 - Art Lubin / Artswork"email = "[email protected]"var set_up_var = doall(script_name, copyright, email)var message = ""var more_message = ""var showmsg = "no"if (set_up_var == 5871){x = x - 1for (var i = 0; i <= x; i++){var messenger = form.elements[i].namemessenger = messenger.substring(0, 2)var fieldname = form.elements[i].namefieldname = fieldname.substring(2){if (messenger == "r_"){more_message = r_check(form,x,fieldname,i)}else if (messenger == "i_"){more_message = i_check(form,x,fieldname,i)}else if (messenger == "d_"){more_message = d_check(form,x,fieldname,i)} if (more_message != "") {if (message == ""){message = more_messagemore_message=""}else{ message = message + "\n" + more_messagemore_message=""} }if (message > ""){showmsg = "yes"}}}if (showmsg == "yes"){alert("The following form field(s) were incomplete or incorrect:\n\n" + message + "\n\n Please complete or correct the form and submit again.")}else{form.submit()}}else{alert ("The copyright information has been changed. \n In order to use this javascript please keep the copyright information intact. \n\n Script Name: Form Validator ver 1.0 \n Copyright: (c) 1998 - Art Lubin / Artswork \n Email: [email protected]")}}function r_check(form,x,fieldname,i){var msg_addition = ""for (var y = 0; y <= x; y++){if (form.elements[y].name == fieldname && form.elements[y].value == ""){msg_addition = form.elements[i].valuebreak}else{msg_addition = ""}}return(msg_addition)}function i_check(form,x,fieldname,i){for (var y = 0; y <= x; y++){if (form.elements[y].name == fieldname)break}var msg_addition = ""inputStr = form.elements[y].value.toString()if (inputStr == ""){msg_addition = form.elements[i].value}else{for (var c = 0; c < inputStr.length; c++){var oneChar = inputStr.charAt(c)if (c == 0 && oneChar == "-"){continue}if (oneChar < "0" || oneChar > "9"){msg_addition = form.elements[i].value}}}return(msg_addition)}function d_check(form,x,fieldname,i)//date must be in MM/DD/YY format OR M/D/YY or a MIX of the two {for (var y = 0; y <= x; y++){if (form.elements[y].name == fieldname)break}var msg_addition = ""var sDate = form.elements[y].valuevar int_or_not = isInteger(form.elements[y].value)if (int_or_not == "true"){if ((!(form.elements[y].value.length >= 6)) || (!(form.elements[y].value.length <= 8))) { msg_addition = form.elements[i].value } else { var SlashlPos = form.elements[y].value.indexOf("/",0)if (SlashlPos > 0 && SlashlPos <= 2){if (SlashlPos == 1){if (form.elements[y].value.charAt(0) < 1 || form.elements[y].value.charAt(0) > 9){msg_addition = form.elements[i].value}else{if ((form.elements[y].value.charAt(0) == 1 || form.elements[y].value.charAt(0) == 3 || form.elements[y].value.charAt(0) == 5 || form.elements[y].value.charAt(0) == 7 || form.elements[y].value.charAt(0) == 8) && ((form.elements[y].value.charAt(2) == 0 && form.elements[y].value.charAt(3) == "/") || (form.elements[y].value.charAt(3) == "/" && form.elements[y].value.length >= 7) || (form.elements[y].value.charAt(1) == "/" && form.elements[y].value.charAt(2) == "/"))){ msg_addition = form.elements[i].value}else if ((form.elements[y].value.charAt(0) == 1 || form.elements[y].value.charAt(0) == 3 || form.elements[y].value.charAt(0) == 5 || form.elements[y].value.charAt(0) == 7 || form.elements[y].value.charAt(0) == 8) && ((form.elements[y].value.charAt(2) >= 3 && form.elements[y].value.charAt(3) > 1) || (form.elements[y].value.charAt(2) == 0 && form.elements[y].value.charAt(3) == 0) || (form.elements[y].value.charAt(1) == "/" && (form.elements[y].value.charAt(3) != "/" && form.elements[y].value.charAt(4) != "/" && form.elements[y].value.charAt(5) != "/" && form.elements[y].value.charAt(6) != "/")))){msg_addition = form.elements[i].value + "hi"}else if ((form.elements[y].value.charAt(0) == 1 || form.elements[y].value.charAt(0) == 3 || form.elements[y].value.charAt(0) == 5 || form.elements[y].value.charAt(0) == 7 || form.elements[y].value.charAt(0) == 8) && (((form.elements[y].value.charAt(2) > 3 && form.elements[y].value.charAt(3) != "/") || (((form.elements[y].value.charAt(1) == "/" && form.elements[y].value.charAt(4) == "/")) && ((form.elements[y].value.length == 6 || form.elements[y].value.length == 8)))) || form.elements[y].value.charAt(5) == "/")){msg_addition = form.elements[i].value}else{if ((form.elements[y].value.charAt(0) == 2 && ((form.elements[y].value.charAt(2) == 0 && form.elements[y].value.charAt(3) == "/") || (form.elements[y].value.charAt(3) == "/" && form.elements[y].value.length >= 7) || (form.elements[y].value.charAt(1) == "/" && form.elements[y].value.charAt(2) == "/") || (form.elements[y].value.charAt(2) == 0 && form.elements[y].value.charAt(3) == 0) || (form.elements[y].value.charAt(1) == "/" && (form.elements[y].value.charAt(3) != "/" && form.elements[y].value.charAt(4) != "/" && form.elements[y].value.charAt(5) != "/" && form.elements[y].value.charAt(6) != "/"))))){msg_addition = form.elements[i].value}else if (form.elements[y].value.charAt(0) == 2 && ((form.elements[y].value.charAt(2) > 2 && form.elements[y].value.charAt(3) != "/") || (((form.elements[y].value.charAt(1) == "/" && form.elements[y].value.charAt(4) == "/") && ((form.elements[y].value.length == 6 || form.elements[y].value.length == 8)))) || form.elements[y].value.charAt(5) == "/")){msg_addition = form.elements[i].value}else{if ((form.elements[y].value.charAt(0) == 4 || form.elements[y].value.charAt(0) == 6 || form.elements[y].value.charAt(0) == 9) && ((form.elements[y].value.charAt(2) == 0 && form.elements[y].value.charAt(3) == "/") || (form.elements[y].value.charAt(3) == "/" && form.elements[y].value.length >= 7) || (form.elements[y].value.charAt(1) == "/" && form.elements[y].value.charAt(2) == "/"))){msg_addition = form.elements[i].value}else if ((form.elements[y].value.charAt(0) == 4 || form.elements[y].value.charAt(0) == 6 || form.elements[y].value.charAt(0) == 9) && ((form.elements[y].value.charAt(2) >= 3 && form.elements[y].value.charAt(3) > 0) || (form.elements[y].value.charAt(2) == 0 && form.elements[y].value.charAt(3) == 0) || (form.elements[y].value.charAt(1) == "/" && (form.elements[y].value.charAt(3) != "/" && form.elements[y].value.charAt(4) != "/" && form.elements[y].value.charAt(5) != "/" && form.elements[y].value.charAt(6) != "/")))){msg_addition = form.elements[i].value}else if ((form.elements[y].value.charAt(0) == 4 || form.elements[y].value.charAt(0) == 6 || form.elements[y].value.charAt(0) == 9) && (((form.elements[y].value.charAt(2) > 3 && form.elements[y].value.charAt(3) != "/") || ((form.elements[y].value.charAt(1) == "/" && form.elements[y].value.charAt(4) == "/") && ((form.elements[y].value.length == 6 || form.elements[y].value.length == 8)))) || form.elements[y].value.charAt(5) == "/")){msg_addition = form.elements[i].value}}}}}else{if (form.elements[y].value.charAt(0) > 1 || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) > 2) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 0)){msg_addition = form.elements[i].value}else{if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 1) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 3) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 5) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 7) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 8) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 0) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 2)) && ((form.elements[y].value.charAt(3) == 0 && form.elements[y].value.charAt(4) == "/") || (form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(3) == "/") || (form.elements[y].value.charAt(2) == "/" && (form.elements[y].value.charAt(4) != "/" && form.elements[y].value.charAt(5) != "/" && form.elements[y].value.charAt(6) != "/" && form.elements[y].value.charAt(7) != "/")))){msg_addition = form.elements[i].value}else if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 1) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 3) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 5) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 7) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 8) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 0) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 2)) && ((form.elements[y].value.charAt(3) >= 3 && form.elements[y].value.charAt(4) > 1) || (form.elements[y].value.charAt(3) == 0 && form.elements[y].value.charAt(4) == 0) || form.elements[y].value.length < 7)){msg_addition = form.elements[i].value}else if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 1) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 3) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 5) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 7) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 8) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 0) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 2)) && ((form.elements[y].value.charAt(3) > 3 && form.elements[y].value.charAt(4) != "/")|| ((form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(5) == "/" && form.elements[y].value.length == 7 || form.elements[y].value.charAt(6) == "/") || (form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(4) == "/" && (form.elements[y].value.length == 6 || form.elements[y].value.length == 8))))){msg_addition = form.elements[i].value}else{if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 2) && ((form.elements[y].value.charAt(3) == 0 && form.elements[y].value.charAt(4) == "/") || (form.elements[y].value.charAt(3) == 0 && form.elements[y].value.charAt(4) == 0)) || form.elements[y].value.length < 7) || (form.elements[y].value.charAt(2) == "/" && (form.elements[y].value.charAt(4) != "/" && form.elements[y].value.charAt(5) != "/" && form.elements[y].value.charAt(6) != "/" && form.elements[y].value.charAt(7) != "/"))){msg_addition = form.elements[i].value}else if ((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 2) && ((form.elements[y].value.charAt(3) > 2 && form.elements[y].value.charAt(4) != "/") || ((form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(5) == "/" && form.elements[y].value.length == 7 || form.elements[y].value.charAt(6) == "/") || (form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(4) == "/" && (form.elements[y].value.length == 6 || form.elements[y].value.length == 8))))){msg_addition = form.elements[i].value}else{if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 4) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 6) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 9) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 1)) && ((form.elements[y].value.charAt(3) == 0 && form.elements[y].value.charAt(4) == "/") || (form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(3) == "/") || (form.elements[y].value.charAt(2) == "/" && (form.elements[y].value.charAt(4) != "/" && form.elements[y].value.charAt(5) != "/" && form.elements[y].value.charAt(6) != "/" && form.elements[y].value.charAt(7) != "/")))){msg_addition = form.elements[i].value}else if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 4) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 6) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 9) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 1)) && ((form.elements[y].value.charAt(3) >= 3 && form.elements[y].value.charAt(4) > 0) || (form.elements[y].value.charAt(3) == 0 && form.elements[y].value.charAt(4) == 0) || form.elements[y].value.length < 7)){msg_addition = form.elements[i].value}else if (((form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 4) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 6) || (form.elements[y].value.charAt(0) == 0 && form.elements[y].value.charAt(1) == 9) || (form.elements[y].value.charAt(0) == 1 && form.elements[y].value.charAt(1) == 1)) && ((form.elements[y].value.charAt(3) > 3 && form.elements[y].value.charAt(4) != "/") || ((form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(5) == "/" && form.elements[y].value.length == 7 || form.elements[y].value.charAt(6) == "/") || (form.elements[y].value.charAt(2) == "/" && form.elements[y].value.charAt(4) == "/" && (form.elements[y].value.length == 6 || form.elements[y].value.length == 8))))){msg_addition = form.elements[i].value}}}}}} else{msg_addition = form.elements[i].value}}}else{msg_addition = form.elements[i].value}return(msg_addition)}function isInteger(sDate){var new_msg = "true"inputStr = sDate.toString()for (var i = 0; i < inputStr.length; i++){var oneChar = inputStr.charAt(i)if ((oneChar < "0" || oneChar > "9") && oneChar != "/"){new_msg = "false"}}return (new_msg)}function doall(script_name, copyright, email){var code = 0var test = script_name + copyright + emailfor (var a = 0; a < test.length; a++){var each_char = test.charAt(a)var x = asc(each_char)code += x}return (code)}function asc(each_char){var n = 0var char_str = charSetStr()for (i = 0; i < char_str.length; i++){if (each_char == char_str.substring(i, i+1)){break}}return i + 32}function charSetStr(){var strstr = ' !"#$%&' + "'" + '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'return str}//--></SCRIPT><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->
<FORM action=validation2.htm><TABLE border=0 width=575> <TBODY> <TR> <TD><B>First Name:</B> </TD> <TD><INPUT name=fname> <INPUT name=r_fname type=hidden value="First name is required"> </TD> <TD><FONT size=-1>(required validation)</FONT></TD></TR> <TR> <TD><B>Last Name:</B> </TD> <TD><INPUT name=lname> <INPUT name=r_lname type=hidden value="Last name is required"> </TD> <TD><FONT size=-1>(required validation)</FONT></TD></TR> <TR> <TD><B>Address 1:</B> </TD> <TD><INPUT name=address1> <INPUT name=r_address1 type=hidden value="Address is required"> </TD> <TD><FONT size=-1>(required validation)</FONT></TD></TR> <TR> <TD><B>Address 2:</B> </TD> <TD><INPUT name=address2> </TD> <TD><FONT size=-1>(no validation)</FONT></TD></TR> <TR> <TD><B>What is your age:</B> </TD> <TD><INPUT name=age> <INPUT name=r_age type=hidden value="Age is required"> <INPUT name=i_age type=hidden value="Age must be an integer"> </TD> <TD><FONT size=-1>(required and integer validation)</FONT></TD></TR> <TR> <TD><B>Years have you been scripting:</B> </TD> <TD><INPUT name=scripting> <INPUT name=r_scripting type=hidden value="Years scripting required"> <INPUT name=i_scripting type=hidden value="Years scripting must be an integer"> </TD> <TD><FONT size=-1>(required and integer validation)</FONT></TD></TR> <TR> <TD><B>Insert the date:</B> <FONT size=-1>(mm/dd/yy)</FONT> </TD> <TD><INPUT name=todays_dt> <INPUT name=r_todays_dt type=hidden value="Today's date is required"> <INPUT name=d_todays_dt type=hidden value="The date is not valid or the format is incorrect. Please format mm/dd/yy."> </TD> <TD><FONT size=-1>(required and date validation)</FONT></TD></TR> <TR> <TD><INPUT onclick=check(form,16) type=button value="Submit this form for validation"></TD> <TD><INPUT type=reset value=Reset></TD></TR></TBODY></TABLE></FORM><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->