Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Validate number

  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    Validate number

    Hi all. I would like to validate a number a user enters into my form. This number can be 5 or 6 numerical numbers or have 6 numeric numbers seperated by a dash with 6 more numeric numbers like so 123456-123456. How can I do this. I'm stumped on this one. Please help me.
    Tracy

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    It would be better to show what you've tried up to now so someone can offer some pointers.

  • #3
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    I actually have nothing. I can validate 5 or 6 numbers but the other one confuses me
    Tracy

  • #4
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    Here is what I have so far but it does not work. Can anyone help?

    Code:
    <script>
    
    function validateCharge(theForm){
    
    //VALIDATE CHARGE
    if(theForm.charge){
     if(!theForm.charge.value.match(/^\d{5}$/) || !theForm.charge.value.match(/^\d{6}$/)){
     alert("Please input a valid Charge Account!")
    theForm.charge.style.backgroundColor='pink';
    theForm.charge.focus();
    return false
    }}
    
    
      return (true);
    
    }
    
    
    </script>
    
    <form method="POST" name="theForm" onsubmit="return validateCharge(theForm)" action="--WEBBOT-SELF--">
     
      <input type="text" name="charge" size="20"></p>
      <p><input type="submit" value="Submit" name="B1"></p>
    </form>

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    /\d{5}|\d{6}(?:-\d{6})?/.test(theForm.charge.value)
    Trinithis

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Trinithis View Post
    Code:
    /\d{5}|\d{6}(?:-\d{6})?/.test(theForm.charge.value)
    No.

    Code:
    if (/^(\d{5,6})$|^(\d{6}-\d{6})$/.test(theForm.charge.value)) {
    James (007) Bond: (to Bibi) "You get your clothes back on, and I'll buy you an ice cream."
    Last edited by Philip M; 05-03-2008 at 08:20 AM. Reason: Add quote


  •  

    Posting Permissions

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