SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast lucky20's Avatar
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validation for numeric values

    Hi everybody,

    I have a problem in asp application validation. My textbox only accepts numeric value..
    It should not accept empty field and negative values and charcters..

    I got it for empty field..
    but not for numeric value..

    Code JavaScript:
    function providerid(fld){
      var error=" "
       if(fld.value== ""){
      fld.style.background='Yellow';
      error= "You didn't enter a product id \n";
      }else{
        fld.style.background='White';
      }return error;
    }
     
    function validateFormOnSubmit(form1) {
      reason += providerid(form1.pid);
      if (reason != "") {
        alert("fields neccessary:\n" + reason);
       return false;
       }
       return true;
    }
    I am not aware of IsNan function..
    Can anyone give some idea about numeric validation..

  2. #2
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    isNaN (is not a number)

    Code:
    console.log (isNaN('abc')); // true
    
    !isNaN -  (!)Not not a number. In other words 'is a number'
    console.log (!isNaN('12')); // true
    console.log (!isNaN('-12')); // true, which you don't want.
    Using a regular expression to test if anything other than 0 to 9 or a dot(decimal place) are present.
    Code:
    var isNum = function(num){return !/[^.[0-9]]*/.test(num);};
    Code:
    console.log(isNum('12.12')); // true
    console.log(isNum('5')); // true
    console.log(isNum('abc')); // false
    console.log(isNum('-12')); // false

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Why not just use the Number object?

    Code javascript:
    If (Number(fld.value) > 0) {
      // is valid
    } else {
     // not valid
    }

    Empty strings will be cast to 0 with the above code, so if you also need 0 to be accepted:


    Code javascript:
    If (fld.value === '0' || Number(fld.value) > 0) {
      // is valid
    } else {
     // not valid
    }
    Last edited by paul_wilkins; Dec 13, 2010 at 17:51.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just use the Number object?]
    Fair enough. I tend to miss the simple solutions.

    Had to read up on the number object and I see that it converts it's argument to a numeric value or returns NaN if it fails.

    Unless I'm missing something '!isNaN(fld.value) && fld.value >= 0' would also do the trick.

    or

    if (Number(fld.value) >= 0)

    edit: both 'if (Number(fld.value) >= 0)' and '!isNaN(fld.value) && fld.value >= 0' fail due to an Empty string returning 0.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by RLM2008 View Post
    Unless I'm missing something '!isNaN(num) && num >= 0' would also do the trick.
    Is there any situation where the isNaN check is needed? Those values will just just fail the condition when checked too, right?

    The only other thing to consider is when an empty string is involved, in which case Number converts that to 0. So you would need a separate check if 0 is a valid number too.

    If you don't intend to allow 0, you can just check that the number is greater than 0 and you're set.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any situation where the isNaN check is needed? Those values will just just fail the condition when checked too, right?
    In answer I don't know. Haven't really used isNaN that much.

    Given that Number returns NaN when it fails, I just thought I'd experiment.

    Number is obviously a simpler and clearer solution.

    The only other thing to consider is when an empty string is involved, in which case Number converts that to 0. So you would need a separate check if 0 is a valid number too.
    Yep missed that.

    Still a lot to learn.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,717
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by RLM2008 View Post
    Number is obviously a simpler and clearer solution.
    Thanks, so it seems that after we've considered various issues, that we're left with something like this:

    Code javascript:
    If (fld.value === '0' || Number(fld.value) > 0) {
      // is valid
    } else {
     // not valid
    }

    or

    Code javascript:
    If (Number(fld.value) > 0) {
      // is valid
    } else {
     // not valid
    }

    Depending on if you want 0 to be allowed or not.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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