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

    validation is getting confused

    The validation i am using is getting confused when it gets a number that has more than three decimal points (ex: 1.000)

    Below is my code - the form works
    Code:
    <script language="javascript">
    function doValidate()
    {
    	var theForm = document.myForm;
    	
    	if (theForm.Field_1.value < theForm.Field_2.value || theForm.Field_1.value == '' || theForm.Field_2.value == '')
    	{
    		alert("You must enter a bigger size");
    		theForm.Value8.focus();
    		return (false);
    	}
    	if (theForm.Field_2.value < theForm.Field_3.value || theForm.Field_2.value == '' || theForm.Field_3.value == '')
    	{
    		alert("You must enter a bigger size");
    		theForm.Value7.focus();
    		return (false);
    	}
    	return (true);
    }
    </script>
    
    <form name="myForm" onSubmit="return doValidate(this)">
    <table>
    	<tr>
        	<th align="right"><span class="style1">Field 1</span></th>
        	<td><input type="text" name="Field_1" size="30"></td>
        </tr>
    	<tr>
        	<th align="right"><span class="style1">Field 2</span></th>
        	<td><input type="text" name="Field_2" size="30"> 
        	  ** </td>
        </tr>
    	<tr>
        	<th align="right"><span class="style1">Field 3</span></th>
        	<td><input type="text" name="Field_3" size="30"> 
        	  ** </td>
        </tr>
    </table>
    </form>
    Last edited by npala2001; 01-09-2007 at 04:51 PM.

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It seems to work to me. Exactly what inputs are failing for you?

    Here is my test code.

    Code:
    <html>
    <head>
    <script language="javascript">
    function doValidate()
    {
    	var theForm = document.myForm;
    	
    	if (theForm.Field_1.value < theForm.Field_2.value || theForm.Field_1.value == '' || theForm.Field_2.value == '')
    	{
    		alert("You must enter a bigger size");
    		// theForm.Value8.focus();
    		return (false);
    	}
    	if (theForm.Field_2.value < theForm.Field_3.value || theForm.Field_2.value == '' || theForm.Field_3.value == '')
    	{
    		alert("You must enter a bigger size");
    		// theForm.Value7.focus();
    		return (false);
    	}
      alert("validated");
    	return (false);
    }
    window.onload = function () {
      document.myForm.Field_1.value = "7.00000";
      document.myForm.Field_2.value = "5.00001";
      document.myForm.Field_3.value = "5.00000";
    }
    </script>
    </head>
    
    <body>
    <form name="myForm" onSubmit="return doValidate(this)">
    <table>
    	<tr>
        	<th align="right"><span class="style1">Field 1</span></th>
        	<td><input type="text" name="Field_1" size="30"></td>
        </tr>
    
    	<tr>
        	<th align="right"><span class="style1">Field 2</span></th>
        	<td><input type="text" name="Field_2" size="30"> 
        	  ** </td>
        </tr>
    	<tr>
        	<th align="right"><span class="style1">Field 3</span></th>
        	<td><input type="text" name="Field_3" size="30"> 
        	  ** </td>
    
        </tr>
    </table>
    <input type="submit" />
    </form>
    </body>
    </html>
    david_kw

  • #3
    New Coder
    Join Date
    Jul 2004
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Field 1 = 12.25

    Field 2 = 9.625

    Field 3 = 5.5

    I have the input types as text fields so I am thinking that is why its treating them differently. It looks like its getting locked up on Field 1 and 2. Is there a way to get the javascript to treat the field as numeric data
    Last edited by npala2001; 01-09-2007 at 05:26 PM.

  • #4
    New Coder
    Join Date
    Jul 2004
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your code is different I have an extra return true statement at the bottom

  • #5
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes you are comparing strings. Of course it would do that...

    Google Javascript and parseInt
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Good call on parseFloat

    Code:
    <html>
    <head>
    <script language="javascript">
    function doValidate()
    {
    	var theForm = document.myForm;
    	
    	if (parseFloat(theForm.Field_1.value) < parseFloat(theForm.Field_2.value) || theForm.Field_1.value == '' || theForm.Field_2.value == '')
    	{
    		alert("You must enter a bigger size");
    		// theForm.Value8.focus();
    		return (false);
    	}
    	if (parseFloat(theForm.Field_2.value) < parseFloat(theForm.Field_3.value) || theForm.Field_2.value == '' || theForm.Field_3.value == '')
    	{
    		alert("You must enter a bigger size");
    		// theForm.Value7.focus();
    		return (false);
    	}
      alert("validated");
    	return (false); // changed from true so it won't reset the page for testing
    }
    window.onload = function () {
      document.myForm.Field_1.value = "12.25";
      document.myForm.Field_2.value = "9.625";
      document.myForm.Field_3.value = "5.5";
    }
    </script>
    </head>
    
    <body>
    <form name="myForm" onSubmit="return doValidate(this)">
    <table>
    	<tr>
        	<th align="right"><span class="style1">Field 1</span></th>
        	<td><input type="text" name="Field_1" size="30"></td>
        </tr>
    	<tr>
        	<th align="right"><span class="style1">Field 2</span></th>
        	<td><input type="text" name="Field_2" size="30"> 
        	  ** </td>
        </tr>
    	<tr>
        	<th align="right"><span class="style1">Field 3</span></th>
        	<td><input type="text" name="Field_3" size="30"> 
        	  ** </td>
        </tr>
    </table>
    <input type="submit" />
    </form>
    </body>
    </html>
    david_kw

  • #7
    New Coder
    Join Date
    Jul 2004
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    MAN I LOVE YOU GUYS!!!! It worked like a champ and I learned something today.

  • #8
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ehehe parseFloat... whoops for some reason I was thinking about Integers... sorry!
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.


  •  

    Posting Permissions

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