regular expression / form validation

Discussion in 'JavaScript' started by texanweb, Mar 31, 2009.

  1. #1
    Hey guys.... I am trying create up a test page for a small project that I am doing for a buddy of mine.

    What I am trying to do is this: use regular expression and have the form validation. Furthermore, I am trying to return the values on the same page. Could yall tell me what I am doing wrong here?

    Thanks,
    Adam


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test Page</title>
    
    
    <script type="text/javascript">
    
    var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
    
    function validate(order){
    var name = order.name.value;
    var errors = [];
     
     if (!ck_name.test(name1)) {
      errors[errors.length] = "You valid Name .";
     }
    
     if (errors.length > 0) {
    
      reportErrors(errors);
      return false;
     }
      return true;
    }
    function reportErrors(errors){
     var msg = "Please Enter Valide Data...\n";
     for (var i = 0; i<errors.length; i++) {
     var numError = i + 1;
      msg += "\n" + numError + ". " + errors[i];
    }
     alert(msg);
    }
    </script>
    </head>
    
    <body>
    
          <form action="submit.html" enctype="multipart/form-data" id="submit1">
    
    
    <table width="250" border="0" cellspacing="5">
                  
                  <tr>
                    <td>First Name:</td>
                    <td><input name="name1" type="text" id="name1" /></td>
                  </tr>
                </table>
              
                <p>
    	      <input type="submit" name="Submit" id="Submit" value="Submit" />&nbsp;&nbsp;&nbsp;
                  <input type="reset" name="Reset" id="Reset" value="Reset" />
            </p>
    </form>
    
    </body>
    </html>
    
    Code (markup):
     
    texanweb, Mar 31, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,
    the code looks fine (at first sight), the only thing is that you never call the validation function. Based on the flow in your function validate() I believe this will help :

    <form action="submit.html" enctype="multipart/form-data" id="submit1" onsubmit="return validate(this)">
     
    lp1051, Mar 31, 2009 IP
  3. texanweb

    texanweb Active Member

    Messages:
    377
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I been able to validate the form, but I still can't return the values and "Receipt a print" on the same page.

    I have this starting my form...
    <form action="submit.html" method="get" enctype="multipart/form-data" id="form" onsubmit="return validate(this)">
     
    texanweb, Apr 1, 2009 IP
  4. manly_ninja

    manly_ninja Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can use Jquery and Form Validation plugin to create validation application easily.
     
    manly_ninja, Apr 6, 2009 IP
  5. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    or you can use this script, Regular Expressions Taster, hxxp://www.javascriptbank.com/javascript/snippet/regular-expressions-taster/preview/

    please replace xx to tt
     
    JavaScriptBank.com, Apr 8, 2009 IP