www.webdeveloper.com
Results 1 to 5 of 5

Thread: Validation Help

  1. #1
    Join Date
    Nov 2002
    Posts
    5

    Question Validation Help

    Hi I am new to Javascript. Is it possible to have a form field that can be submitted empty but when it is entered it must be a 6 digit number only.

    I have experimented with onKeypress in the input field and it work great:

    onKeypress="if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;"

    I have found a few scripts that can make the length 6 digits long but won't let an empty field submit.

    Any help here would be very much appreciated

  2. #2
    Join Date
    Nov 2002
    Posts
    5
    here it is a good idea to use a regular expression.

    here is a function that will do the job :

    Code:
    function validate() {
       var re = /^(\d{6})$/;  //reg exp that checks for six digits 
       if(document.form1.foo.value.length>0) {
         if(!re.test(document.form1.foo.value)) {
          alert("six digits please!")
         }
       }
    }
    Last edited by tobake; 11-27-2002 at 02:46 AM.

  3. #3
    Join Date
    Nov 2002
    Posts
    5

    Return focus

    Thank very much tobake it works great. I have one more question: how do I return the focus to the field that does not have the six digits. This script continues to submit after the alert button is clicked.

    I have played with the script a bit but am not experienced enough to know what I am doing.


    function validate() {
    var re = /^(\d{6})$/; //reg exp that
    if(document.order.parcel1.value!="") {
    if(!re.test(document.order.parcel1.value)) {
    alert("six digits please!")
    field.focus();
    field.select();
    return false;
    }
    }
    }

  4. #4
    Join Date
    Nov 2002
    Posts
    5
    Code:
    document.order.parcel1.select()
    Use select() if you want to select the text in the field or focus() i you only want to give focus to the field.

  5. #5
    Join Date
    Nov 2002
    Posts
    5
    Thank you very much tobake for your time and help!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles