Validate Data Before Submit using Regular Expressions : Form Data Validation « JSP « Java Tutorial






index.jsp

<HTML>
    <HEAD>
        <TITLE>Verifying User Data</TITLE>
        <SCRIPT LANGUAGE="JavaScript">
            <!--
            function checker()
            {
                var regExp1 = /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/
                var regExp2 = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/
                var result1 = document.form1.text1.value.match(regExp1)
                var result2 = document.form1.text1.value.match(regExp2)
                if (result1 == null && result2 == null) {
                    alert("Sorry, that's not a valid date.")
                    document.form1.text1.value = ""
                    return false
                } else {
                    document.form1.submit()
                }
            }
            //-->
        </SCRIPT>
    </HEAD>
 
    <BODY>
        <H1>Verifying User Data</H1>
        <FORM NAME="form1" ACTION="basic.jsp" METHOD="POST" ONSUBMIT="return checker()">
            Please enter a date:
            <INPUT TYPE="TEXT" NAME="text1">
            <INPUT TYPE="SUBMIT" value="Submit">
        </FORM>
    </BODY>
<HTML>

basic.jsp

<HTML>
  <HEAD>
    <TITLE>Reading Verified Data</TITLE>
  </HEAD>

    <BODY>
        <H1>Reading Verified Data</H1>
        That date is 
        <% out.println(request.getParameter("text1")); %>
   </BODY>
</HTML>
  Download:  ValidateDataBeforeSubmit.zip( 1 k)








23.24.Form Data Validation
23.24.1.Validate Data Before Submit using Regular Expressions