Form Validation

This script allows you to have your forms checked or validated before it is submitted.  Validation helps prevent an incomplete form from being submitted. The JavaScript checks the form on the client side which makes the response instantaneous and will not allow the form to submit until all requirements are met.  Take a look at a sample and then see the script below that made it possible.

Sample | Back to Web Tips


<script language="JavaScript">
<!--
var dataOK=false
function
checkData( )
{

if (document.form_name.field_name.value.length <= 1) {
alert("This is a required field, please enter a value")
return false}

return true
}
//-->
</script>

<FORM ACTION="/cgi-bin/mailback.cgi" METHOD=POST  NAME="form_name"
onSubmit="return checkData( )"
>
<INPUT TYPE="hidden" NAME="fwd_to" VALUE="YourAddress@YourDomain.com">
<INPUT TYPE="hidden" NAME="via_html" VALUE="orderform.htm">
<INPUT TYPE="hidden" NAME="nexturl" VALUE="http://www.YourDomain.com/thankyou.htm">
<INPUT TYPE="hidden" NAME="subject" VALUE="E-Mail Subject">


  • Enter this script anywhere in your HTML page, preferably between the <Head> tags or anywhere near your form entries. JavaScript is case-sensitive so make sure your field names correspond to this script exactly.
  • Form Name is the name of the form you specified as noted above.  Do not use a space in the name, instead use an underscore.
  • Field Name is the name you assign to the text field, such as "Full_Name" ( Note: Do not use spaces in the field name, instead use an underscore ).
  • You can place any message between the quotes in the alert line
  • The red lines in the script must be repeated in its entirety for every field you want to validate.
  • You can use different equality checks after value.length for more control. The valid conditional operators are:
    equal (==), not equal (!=), greater than (>), less than (<),
    greater than or equal (>=), and less than or equal (<=)

Back to Web Tips