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 2 of 2

Thread: Validation

  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validation

    how how do i do simple validation on textboes on a website so that when a button is press to post a form and it is not coorect a message box appears.

    in to validate a 2 things
    1 A number between 1 and 3 digits
    2 A time eg 13.00

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well it would be nice to see your textboxes, but:
    Code:
    function validateNumber(b) {
     var n = b.value*1;
     return (n >= 1 && n <= 3);
    }
    
    function validateTime(b) {
     var t = b.value;
     var t_array = t.split(".");
    
     var hour = t_array[0]*1;
     var mins = t_array[1]*1;
    
     return(hour >= 0 && hour <= 23 && mins >= 0 && mins <= 59);
    }
    
    function validateBoxes() {
     if(!validateNumber(numBox)) {
      alert("Bad Number");
     }
    
     if(!validateTime(timeBox)) {
      alert("Bad Time");
     }
    }
    
    <input type="text" name="numBox" id="numBox"/>
    <input type="text" name="timeBox" id="timeBox"/>
    <input type="button" onClick="validateBoxes"/>
    I think that does what you're looking for. I'm sure you could do something with regular expressions too, but this way makes sense to me.

    Hope that helps,
    Sadiq.


  •  

    Posting Permissions

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