validation of date and set focus problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MickG

    #1

    validation of date and set focus problem

    Hi,

    I am trying to validate these values, this seems to work fine for the
    phone number and name but I am trying to get the program to fail to
    submit and set the focus on the date when 2006 is selected and
    submitted.

    Thanks in advance for any help.


    <HTML>
    <head>
    <link rel="stylesheet " type="text/css" href="test.css"/>
    <script language="JavaS cript">
    <!------------------------------------GENERIC
    TEST-------------------------------------------------------------->
    function genericTest(any RE,title,string ToTest) {
    if(anyRE.test(s tringToTest)) {
    return true;
    } else {
    window.alert("T he " + title +" format of:\n" + stringToTest + "\nwas
    not recognised.");
    return false;
    }
    }

    <!------------------------------------CHECK
    WHOLE-------------------------------------------------------------->
    function checkWhole() {
    /*
    if OK variable is false, skip subsequent tests so user can
    correct entries one at a time in the same order as on the form
    */
    OK = checkFullName(d ocument.form1.C CName.value);
    OK = checkDate(docum ent.form1.CCEYe ar.value);
    if ( OK ) OK = genericTest(pho neRE,"telephone
    number",documen t.form1.phone.v alue);

    return OK; // OK false if any ONE (or more) test fails
    }

    <!------------------------------------CHECK FULL
    NAME-------------------------------------------------------------->
    // Reject full name if less than 5 characters
    function checkFullName (fn) {
    if(fn.length < 5) { // reject missing or full name less than 5
    characters
    window.alert("T he full name format:\n" + fn + "\nwas not recognised
    (missing or too short).");
    return false;
    } else {
    return true;
    }
    }

    function checkDate(val){
    if(val==2006){
    window.alert("I nvalid Year");

    return false;
    } else {
    return true;
    }
    }

    /*PHONE RE*/
    phoneRE = /^(\+|\s)*\d(\d| \s)*(\(\s*\d(\d |\s)*\)|(\d|\s) *)(\d|\s)+(x\s* \d(\d|\s)*|ext\ s*\d(\d|\s)*)?$/i;


    /*friendly_email RE*/

    friendly_eMailR E = /^\s*[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}\s*$/i;

    /*this e-mail address RE is COMPUTER friendly because it rejects all
    spaces*/
    eMailRE = /^[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}$/i;



    </script>
    </head>


    <BODY>
    <IMG SRC="logo.gif"> <BR>
    <H1>Order Form</H1>
    <HR>
    <FORM NAME="form1" method="post"
    action="https://media.paisley.a c.uk/cgi-davison/shopMail.cgi"
    onSubmit="retur n checkWhole();">
    <table border=0 ><tr>

    </tr>
    <th>Phone</th><th><INPUT TYPE="TEXT" NAME="phone" SIZE=25
    onChange='dummy =genericTest(ph oneRE,"telephon e
    number",this.va lue);'></th>
    </tr>
    <tr>
    <th>
    </tr>
    <tr>
    <th>CCName</th><th><INPUT TYPE="TEXT" NAME="CCName" SIZE=30
    onChange='dummy =checkFullName( this.value);'></th>

    <tr>
    <th>Card ExpiryYear</th>
    <th><select name="CCEYear" tabindex="1" ;>
    <option value= "invalid" SELECTED > Select Year</option>
    <option vale="2005" >2005</option>
    <option vale="2006" >2006</option>
    <option vale="2007" >2007</option>
    <option vale="2008" >2008</option>

    </select></th>


    </table>

    <input type="hidden" name="recipient " value="abc@hotm ail.com">


    </SCRIPT>
    </TABLE>
    <INPUT TYPE="SUBMIT" VALUE="Submit Order">
    </FORM>
    </BODY>
    </HTML>
  • McKirahan

    #2
    Re: validation of date and set focus problem

    "MickG" <mickg77@hotmai l.com> wrote in message
    news:1a9c775a.0 503300149.781e7 33@posting.goog le.com...[color=blue]
    > Hi,
    >
    > I am trying to validate these values, this seems to work fine for the
    > phone number and name but I am trying to get the program to fail to
    > submit and set the focus on the date when 2006 is selected and
    > submitted.
    >
    > Thanks in advance for any help.[/color]

    "value=" not "vale=".

    <option vale="2005" >2005</option>
    <option vale="2006" >2006</option>
    <option vale="2007" >2007</option>
    <option vale="2008" >2008</option>


    Comment

    • mickg77@hotmail.com

      #3
      Re: validation of date and set focus problem

      thanks, sometimes you need another pair of eyes, but i'm still trying
      to get the focus() working after the validation.

      again, any help would be greatly appreciated

      Comment

      • McKirahan

        #4
        Re: validation of date and set focus problem

        <mickg77@hotmai l.com> wrote in message
        news:1112180733 .063068.185200@ o13g2000cwo.goo glegroups.com.. .[color=blue]
        > thanks, sometimes you need another pair of eyes, but i'm still trying
        > to get the focus() working after the validation.
        >
        > again, any help would be greatly appreciated[/color]

        Change
        OK = checkDate(docum ent.form1.CCEYe ar.value);
        to
        OK = checkDate(docum ent.form1.CCEYe ar);
        then change
        if(val==2006) {
        to
        if(val.value==2 006) {
        val.focus();

        Also, your "function checkWhole()"
        doesn't fo what you say it will do; specifically,

        /*
        if OK variable is false, skip subsequent tests so user can
        correct entries one at a time in the same order as on the form
        */

        What's with all the references to:
        onchange="dummy =.." ?


        Comment

        • Fred Oz

          #5
          Re: validation of date and set focus problem

          MickG wrote:[color=blue]
          > Hi,
          >
          > I am trying to validate these values, this seems to work fine for the
          > phone number and name but I am trying to get the program to fail to
          > submit and set the focus on the date when 2006 is selected and
          > submitted.
          >
          > Thanks in advance for any help.
          >
          >
          > <HTML>
          > <head>
          > <link rel="stylesheet " type="text/css" href="test.css"/>
          > <script language="JavaS cript">[/color]

          Language attribute for script elements is depreciated, use type:

          <script type="text/javascript">

          Do not post code with tabs. Replace all tabs with 2 or 4 spaces and
          manually wrap lines at about 70 characters to prevent automatic
          wrapping which introduces errors into your code.

          Your code and script is riddled with errors, I'll point out a few but
          not all. Run your code through a validator (there's a link to the
          free w3c validator below):

          <URL:http://validator.w3.or g/>

          [color=blue]
          > <!------------------------------------GENERIC
          > TEST-------------------------------------------------------------->[/color]

          This is an HTML comment and will cause an error in JavaScript.

          /* --------- GENERIC TEST ---------*/
          [color=blue]
          > function genericTest(any RE,title,string ToTest) {
          > if(anyRE.test(s tringToTest)) {
          > return true;
          > } else {
          > window.alert("T he " + title +" format of:\n" + stringToTest + "\nwas
          > not recognised.");
          > return false;
          > }
          > }[/color]

          Presuming the 'if' logic is OK, it can be more simply written as:

          function genericTest(any RE,title,string ToTest) {
          if( !anyRE.test(str ingToTest) ) {
          alert('The ' + title +' format of:\n'
          + stringToTest + '\nwas not recognised.');
          return false;
          }
          }

          There is no need to explicitly return true, that will happen anyway if
          the script successfully completes.
          [color=blue]
          >
          > <!------------------------------------CHECK
          > WHOLE-------------------------------------------------------------->[/color]

          /* -------- CHECK WHOLE -------- */

          [color=blue]
          > function checkWhole() {
          > /*
          > if OK variable is false, skip subsequent tests so user can
          > correct entries one at a time in the same order as on the form
          > */
          > OK = checkFullName(d ocument.form1.C CName.value);
          > OK = checkDate(docum ent.form1.CCEYe ar.value);[/color]

          If checkFullName returns false, then checkDate returns true, you will
          not detect the 'invalid' date because OK will be reset to true.

          Returning errors one at a time like this is very tiresome for users,
          better to build a single message and write all errors to the page so
          they can be fixed, but I'll leave that up to you.
          [color=blue]
          > if ( OK ) OK = genericTest(pho neRE,"telephone
          > number",documen t.form1.phone.v alue);
          >
          > return OK; // OK false if any ONE (or more) test fails[/color]

          All of the above can be replaced with:

          if (! checkFullName(d ocument.form1.C CName.value) {
          alert("Fix full name");
          return false;
          }
          if (! checkDate(docum ent.form1.CCEYe ar.value) {
          alert("Fix date");
          return false;
          }
          if (! genericTest(pho neRE,"telephone number",
          document.form1. phone.value) {
          alert("Fix phone number");
          return false;
          }

          Note that automatic wrapping breaks your code even more.
          [color=blue]
          > }
          >
          > <!------------------------------------CHECK FULL
          > NAME-------------------------------------------------------------->[/color]

          /* -------- CHECK FULL NAME -------- */
          [color=blue]
          > // Reject full name if less than 5 characters
          > function checkFullName (fn) {
          > if(fn.length < 5) { // reject missing or full name less than 5
          > characters
          > window.alert("T he full name format:\n" + fn + "\nwas not recognised
          > (missing or too short).");
          > return false;
          > } else {
          > return true;
          > }[/color]

          No need for the 'else', just return false if your test returns true
          and if otherwise, the script will successfully complete and return
          true.
          [color=blue]
          > }
          >
          > function checkDate(val){
          > if(val==2006){
          > window.alert("I nvalid Year");
          >
          > return false;
          > } else {
          > return true;
          > }[/color]

          Same here.
          [color=blue]
          > }
          >
          > /*PHONE RE*/
          > phoneRE = /^(\+|\s)*\d(\d| \s)*(\(\s*\d(\d |\s)*\)|(\d|\s) *)(\d|\s)+(x\s* \d(\d|\s)*|ext\ s*\d(\d|\s)*)?$/i;
          >
          >
          > /*friendly_email RE*/
          >
          > friendly_eMailR E = /^\s*[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}\s*$/i;
          >
          > /*this e-mail address RE is COMPUTER friendly because it rejects all
          > spaces*/
          > eMailRE = /^[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}$/i;
          >
          >[/color]

          I have no idea if these RegExps work, I didn't get your page to work,
          look like they are not appropriate. Search this group for better
          expressions, I think some were posted recently (within the last
          month).

          If you are ever going to test formats, you *must* tell the user what
          format you expect or they are simply guessing in the dark. I also
          don't see why these have to be global variables, surely each needs to
          be used just once inside a single test module?
          [color=blue]
          >
          > </script>
          > </head>
          >
          >
          > <BODY>
          > <IMG SRC="logo.gif"> <BR>
          > <H1>Order Form</H1>
          > <HR>
          > <FORM NAME="form1" method="post"
          > action="https://media.paisley.a c.uk/cgi-davison/shopMail.cgi"
          > onSubmit="retur n checkWhole();">
          > <table border=0 ><tr>
          >
          > </tr>[/color]

          An empty row?
          [color=blue]
          > <th>Phone</th><th><INPUT TYPE="TEXT" NAME="phone" SIZE=25
          > onChange='dummy =genericTest(ph oneRE,"telephon e
          > number",this.va lue);'></th>[/color]

          th tags outside a row? th is meant for a header row, you've used it
          like a general cell tag. Use td unless you really mean th.
          [color=blue]
          > </tr>[/color]

          Closing tr without an opening tr.
          [color=blue]
          > <tr>
          > <th>
          > </tr>[/color]

          No closing th? Only one cell with the above row (probably) has two
          cells? No span attribute?
          [color=blue]
          > <tr>
          > <th>CCName</th><th><INPUT TYPE="TEXT" NAME="CCName" SIZE=30
          > onChange='dummy =checkFullName( this.value);'></th>[/color]

          You assign the return value of checkFullName to the global
          variable 'dummy'; but don't use dummy anywhere. If there is no need
          for dummy, why not:

          <td>CCName</td>
          <td><INPUT TYPE="TEXT" NAME="CCName" SIZE="30" onChange="
          checkFullName(t his.value);
          ">
          </td>

          Doing validation 'onchange' is not nice as nothing happens until the
          user clicks on the next input (i.e. the 'changed' input loses focus).
          They've moved on, but the validation drags them back to an input they
          thought they'd completed.

          Not only that, but they get trapped in an endless loop of alerts
          until they enter a valid value - and you don't even give a hint as to
          what that might be. Their only other recourse is to kill the
          browser.

          Better to just do validation on submit.
          [color=blue]
          >
          > <tr>
          > <th>Card ExpiryYear</th>
          > <th><select name="CCEYear" tabindex="1" ;>[/color]

          The semi-colon at the end is redundant, it probably wont cause an
          error but it isn't needed.
          [color=blue]
          > <option value= "invalid" SELECTED > Select Year</option>
          > <option vale="2005" >2005</option>
          > <option vale="2006" >2006</option>
          > <option vale="2007" >2007</option>
          > <option vale="2008" >2008</option>
          >
          > </select></th>
          >[/color]

          More invalid script - no closing tr? Your 'vale' error has already
          been pointed out. If the value and the text are the same, just use
          the text as the value will be the text (or so the spec claims and
          unless testing proves otherwise).
          [color=blue]
          >
          > </table>
          >
          > <input type="hidden" name="recipient " value="abc@hotm ail.com">
          >
          >
          > </SCRIPT>
          > </TABLE>[/color]

          These tags seem way out of place, ditch 'em.
          [color=blue]
          > <INPUT TYPE="SUBMIT" VALUE="Submit Order">
          > </FORM>
          > </BODY>
          > </HTML>[/color]

          To get the focus back on your 'CCEYear' form element:

          if (document.form1 .CCEYear.blur() ) {
          document.form1. CCEYear.blur();
          }

          That is not the full extent of your errors, but I don't have time to
          find or fix them all.

          I suggest you start by including one input and one script and get
          that working, then build up slowly until it all works and validates.


          --
          Fred

          Comment

          • mickg77@hotmail.com

            #6
            Re: validation of date and set focus problem

            thanks for the help, I'll get to it.

            Michael

            Comment

            • Fred Oz

              #7
              Re: validation of date and set focus problem

              mickg77@hotmail .com wrote:[color=blue]
              > thanks for the help, I'll get to it.
              >
              > Michael
              >[/color]

              Here is a head start - I missled you on the return true thing. If
              you don't sepecify a value, functions that complete return
              'undefined', which is not false (duh!) unless you specify otherwise.
              The script below provides a skeleton to get going, use what you like.

              Lightly commented, tested in Safari and Firefox on Mac OS X.

              <HTML>
              <head><title>st uff</title></head>
              <BODY>

              <script type="text/javascript">
              function validateForm(f) {
              var ph = f.phone.value;
              var ccn = f.CCName.value;
              var yr = f.CCEYear.value ;
              var msg = [];
              var t;

              // Test phone number
              // Format is 8 digits
              if ( (t=testPH(ph)) && 'undefined' != typeof t ) {
              msg.push(t);
              }

              // Test credit card name
              // Must be more than 5 characters without spaces
              if ( (t=testCCN(ccn) ) && 'undefined' != typeof t) {
              msg.push(t);
              }

              // Test year
              // must not be 2006 or not a number (see note below)
              if ( (t=testYR(yr)) && 'undefined' != typeof t) {
              msg.push(t);
              }

              // If we got any errors, write them & return false
              if ( 0 != msg.length ){
              writeMsg(msg.jo in('<br>'));
              return false;
              }
              }

              function testPH(p) {
              // remove spaces, check if 8 digits and 8 long
              // you may want a different test here
              p = p.replace(/\s*/g,'');
              if (!/\d{8}/.test(p) || 8 != p.length ) {
              return '<b>Phone number must be 8 digits</b>';
              }
              }

              function testCCN(c) {
              // remove spaces, check if 5 characters of any kind
              // you may want a better test here
              var x = c.replace(/\s*/g,'');
              if (x.length < 5) {
              return '<b>Name must be at least 5 characters,'
              +' not counting the spaces</b>';
              }
              }


              function testYR(y) {
              // check if y is 2006 or not a number
              // Note: only use isNaN here because value is from conrolled list
              // Otherwise more robust validation is required.
              if ( 2006 == y || isNaN(y) ) {
              return '<b>Year can\'t be ' + y + '</b>'
              }
              }

              function checkForm(f) {
              // Run validate from a button rather than onclick
              if ('undefined' == typeof validateForm(f) ){
              var txt = 'Values are all OK';
              writeMsg(txt)
              }
              }

              function clearErr(f){
              // Just clears error messages
              writeMsg('');
              }

              function writeMsg(m){
              // Generic write function. If getElementById not supported,
              // Presents error in an alert after repalcing <br> with \n
              // and removing all other markup
              if (document.getEl ementById) {
              document.getEle mentById('eSpan ').innerHTML = m;
              } else {
              alert(m.replace (/<br>/g,'\n').replace (/<[^<>]*>/ig,''));
              }
              }

              </script>
              <H1>Order Form</H1>
              <HR>
              <FORM NAME="form1" action="" onSubmit="retur n validateForm(th is);
              return false;">
              <table border="0">
              <tr>
              <td>Phone</td>
              <td>
              <INPUT TYPE="TEXT" NAME="phone" SIZE="25"
              value="1234 5678"><br>
              <i>nnnn nnnn</i>
              </td>
              </tr>
              <tr>
              <td>CCName</td>
              <td>
              <INPUT TYPE="TEXT" NAME="CCName" SIZE="30"
              value="asdfasdf "><br>
              <i>Must be more than 5 characters</i>
              </td>
              </tr>
              <tr>
              <td>Card ExpiryYear</td>
              <td>
              <select name="CCEYear">
              <option SELECTED value="default" >Select Year</option>
              <option>2005</option>
              <option>2006</option>
              <option>2007</option>
              <option>2008</option>
              </select>
              </td>
              </tr>
              </table>
              <INPUT TYPE="button" VALUE="check form"
              onclick="checkF orm(this.form); ">
              <INPUT TYPE="reset" onclick="clearE rr(this.form);
              this.form.reset ();">
              <INPUT TYPE="SUBMIT" VALUE="Submit Order">
              </FORM>
              <span id="eSpan"></span>
              </BODY>
              </HTML>


              --
              Fred

              Comment

              Working...