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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2004
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Field Comparison

    Hi guys I am new to javascript!

    I have three fields that I want validated on a form. Item3 cannot be smaller than Item2 and Item2 cannot be smaller than Item1.

    Below is what I had in mind:

    Function validateForm()
    {
    If (Item3 < Item2)
    alert="You must enter a bigger size for Item 3"
    else
    If (Item2 < Item1)
    alert="You must enter a bigger size for Item 2
    }
    </script>

    <form method="POST" action="Post.jsp" onsubmit="validateForm(this)">

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    1. elements(objects) should be properly refered. A form's element is to be found, usually (but there are some other methods as well) as an element of the form

    document.forms['formname'].elements['elementname']

    2. fields' input content is controlled by the value attribute. this attribute returns always a string, not a number, so that before any Math operation or compare, the return result must be transformed in a number. One of the methods is Number().

    3. Usualy, the validate follows this general pattern:

    ...
    function validate(){
    if(... condition is not fulfiled...){
    alert('Hey, the condition is not fulfiled!')
    return false;
    }
    }
    .....
    <form onsubmit="return validate()">
    ...

    Now try to play with this and let us know what you have done.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Jul 2004
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm very interesting. Why was this posted twice?

  • #4
    New Coder
    Join Date
    Jul 2004
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you have any links to online resources regarding this subject?

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    the usual javascript online tutorials... Nothing special.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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