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

Thread: Form validation

  1. #1
    Abd
    Abd is offline
    New Coder
    Join Date
    Aug 2002
    Location
    Lagos, Nigeria
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Form validation

    Hi All

    I need a help on client side validation. I have several textboxes in my form, and I want to add(sum) the content of two textboxes and store it in a variable. I want the content of that variable to be compared ( > OR <) than the third textbox. If yes then the form should not be submitetd.


    Thanks
    Abdul

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    hope this helps

    m_n

    Code:
    <html>
    <head>
    <title>sum test</title>
    <script>
    
    function checkSum() {
    var num1 = document.myForm.firstNumber.value;
    var num2 = document.myForm.secondNumber.value;
    var comp = document.myForm.compareNumber.value;
      if( isNaN(num1) || isNaN(num2) || isNaN(comp) || num1.length<1 || num2.length<1 || comp.length<1 ) {
        alert( 'error: all fields must have a numeric value' );
    	return false;
      }
      
       
      if( parseInt(num1) + parseInt(num2) == parseInt(comp) ) {
        alert( 'ok to submit' );
    	return true;
      }
      else {
        alert( 'cannot submit' );
    	return false;
      }
    }
    </script>
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000">
    <form name="myForm" action="myPage.html" method="post" onSubmit="return checkSum()">
    first number:<br><input type="text" name="firstNumber" maxlength="10" value="0"><br><br>
    second number:<br><input type="text" name="secondNumber" maxlength="10" value="0"><br><br>
    compare number:<br>
    <input type="text" name="compareNumber" value="0"><br><br>
    <input type="submit" name="myButton" value="submit">
    </form>
    </body>
    </html>
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Abd
    Abd is offline
    New Coder
    Join Date
    Aug 2002
    Location
    Lagos, Nigeria
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It works

    Hi Tommy Cooper,

    Thanks it works


    Abd
    Abdul


  •  

    Posting Permissions

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