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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NaN and defining variables

    hi i want to check a form field for being non-numeric.
    i have this currently
    function Validate(theForm) {
    var missing = "";
    if (isNaN(theform.quantity.value)) missing += "\n - Quantity must be a number\n";
    if (theForm.quantity.value == "") missing += "\n - Please enter a value for quantity\n";
    if (theForm.quantity.value > 2000) missing += "\n - Quantity must be below 2000 for online quotations. Please call 0800876876876 for a quote\n";

    if (missing != "") {
    alert("The following errors occurred\n"
    + missing
    + "\nPlease complete and resubmit");
    return false;
    } else return true;


    }


    the nan isnt being picked up. is this the correct way to reference to a form value using the NaN function

    thanks

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your form quantity value is a string. You must convert that to a number which you can do by multiplying by 1.

    if (isNaN(theform.quantity.value*1))

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If no entry is made in the form element I don't believe that isNaN will pick that up. You might try using regular expressions to validate. Here is a link to a tutorial on the subject:
    http://www.javascriptkit.com/javatutors/re.shtml

    another one:
    http://www.webreference.com/js/column5/index.html

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ooops;

    I didn't read the entire code, only the first line which dealt with the isNaN question, but RequestCode is correct in that if the field is empty, both conditions will be met (not a number and empty). You can try changing that line to:

    Code:
    if (!theform.quantity.value=='' && isNaN(theform.quantity.value*1))

  • #5
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have tried this
    if (!theform.quantity.value=='' && isNaN(theform.quantity.value*1)) missing += "\n - Quantity must be a number\n";

    and it is bypassing this and not showing any error message???

    any ideas?

    thanks

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What do you mean it doesn't work? It works for me....

    Code:
    <script>
    function test(theform){
    var missing = '';
    if (!theform.quantity.value=='' && isNaN(theform.quantity.value*1)) missing += "\n - Quantity must be a number\n";
    alert(missing)
    }
    </script>
    
    <form>
    <input type="text" name="quantity" onchange="test(this.form)">
    </form>

  • #7
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check your cases! You are using both theform & theForm

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    This is quite confusing

    if (!theform.quantity.value=='' && ...

    Why not just

    if (theform.quantity.value!='' && ...

    ?

    and you don't need to multiply it with 1.

    if (theform.quantity.value!='' && isNaN(theform.quantity.value)) {
    missing += "\n - Quantity must be a number\n";
    alert(missing)
    }

    If quantity is required, then you can make the condition like this:

    if (theform.quantity.value=='' || isNaN(theform.quantity.value)) {
    missing += "\n - Quantity must be a number\n";
    alert(missing)
    }

  • #9
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    This is quite confusing

    if (!theform.quantity.value=='' && ...
    you find not equal to confusing?

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Yes, but because of the == and not of the ! operator. When you see ==, you tend to overlooked the ! at the start because that's not common. IMO, this is the common way and more simplified version and direct to the point.

    if (theform.quantity.value!='' &&

    Isn't that better?


  •  

    Posting Permissions

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