Web Analytics Made Easy -
StatCounter phone validation - CodingForum

Announcement

Collapse
No announcement yet.

phone validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • phone validation

    Hi
    The following is a simple form with text boxes..Used java script to check for nulls but can someone give me some help as how to check my phone number for only integer values ie numbers,would be really thankful if some help comes
    thank you
    <SCRIPT type="text/javascript">
    function checkform(){
    if(document.form1.street1.value==""){
    alert('Please fill all the required fields');
    return false;
    }
    if(document.form1.street2.value==""){
    alert('Please fill all the required fields');
    return false;
    }
    if(document.form1.city.value==""){
    alert('Please fill all the required fields');
    return false;
    }
    if(document.form1.phone.value==""){
    alert('Please fill all the required fields');
    return false;
    }
    </script>
    <form name=form1 action="/buynow/payment.dot" onSubmit="return checkform()">
    Street1:

    <input type="text" name="street1" value=" "/>
    <br />
    Street2:

    <input type="text" name="street2" value=""/>
    <br/>
    City:

    <input type="text" name="city" value=""/>
    <br/>
    Phone:<input type="text" name="shippingphone" value=""/><br>
    <INPUT type="submit" name="mysubmit" value="Submit">
    </form>
    </form>

  • #2
    Hi mate,

    you could always use an input mask to limit the field so that only numbers can be typed.

    The following goes in the head of the doc:

    Code:
          function inputMask(e,charsAllowed)
    //validates textbox input against allowed characters
          {
            if (charsAllowed.indexOf(String.fromCharCode(e.keyCode)) == -1)
              return false
          }
    Then put in the form:

    Code:
    Phone:<input type="text" name="shippingphone" onkeypress='javascript:return inputMask(event,"1234567890")' />
    You can then limit the field to whatever input you wish, as above only the numbers 0-9 can be typed. Otherwise you may need a validation function to be called when you press the submit button. I use a simple validation code that links to a VB script to confirm if numbers or dates. However, if it's only numbers to be typed, then the above solves any validation problems for me.

    Hope this helps.

    Comment


    • #3
      Originally posted by longman View Post
      you could always use an input mask to limit the field so that only numbers can be typed.
      The problem with this method is that the user can paste inappropriate values into the textbox.

      So you really need to validate the phone number onsubmit as well.

      Code:
      if (!/^(\d{10})$/.test(document.form1.phone.value)) {
      alert ("Please fill a 10-digit phone number");
      document.form1.phone.value = "";
      document.form1.phone.focus();
      return false;
      }

      Note that "validation" taking the form of
      if(document.form1.street1.value==""){
      is barely worthy of the name, as any entry - even a single space or a ? - will pass the validation.

      You should also be aware that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling JavaScript.

      I notice that in the HTML you have
      Phone:<input type="text" name="shippingphone" value=""/><br>
      but in the script you have
      if(document.form1.phone.value==""){
      Naturally the names must be the same.


      BTW, please follow the posting guidelines and wrap your code in [code] tags. This means use the octothorpe # button on the toolbar. The word [CXDE] (change the X to an O) should be in capitals and within square brackets, but of course if I do that here it will wrap code tags around this paragraph.





      “In ancient times they had no statistics, so they had to fall back on lies” — Stephen B Leacock
      Last edited by Philip M; Apr 8, 2009, 06:55 AM. Reason: Note re = ""

      All the code given in this post has been tested and is intended to address the question asked.
      Unless stated otherwise it is not just a demonstration.

      Comment

      Working...
      X