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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JS doesn't check for blank field value, can you fix it for me?

    My Javascript doesn't display an error when the "bid" field is blank but I want it to. Can someone help me fix it? Everything else works okay but I'm not a Javascript programmer so I don't know where to begin.
    Code:
    function checkBid(fieldName,minValue,maxValue){
       var numberfield = fieldName;
    
       if (chkNumeric(fieldName,minValue,maxValue) == false){
          numberfield.select();
          numberfield.focus();
          return false;
       }
       else{
          return true;
       }
    }
    
    function chkNumeric(fieldName,minValue,maxValue){
       var checkOK = "0123456789";
       var checkStr = fieldName;
       var allValid = true;
       var decPoints = 0;
       var allNum = "";
    
       for (i = 0;  i < checkStr.value.length;  i++){
          ch = checkStr.value.charAt(i);
       for (j = 0;  j < checkOK.length;  j++)
    
       if (ch == checkOK.charAt(j))
          break;
       if (j == checkOK.length){
          allValid = false;
          break;
       }
       if (ch != ",")
          allNum += ch;
       }
       if (!allValid){	
          alertsay = "The bid field contains one or more invalid characters:\nPlease use only numeric digits in this field ...\n "
          alert(alertsay);
          return (false);
    }
    
       var chkVal = allNum;
       var prsVal = parseInt(allNum);
    
       if (chkVal != "" && !(prsVal >= minValue && prsVal <= maxValue)){
       alertsay = "Your bid value is outside the acceptable bidding range:\nPlease enter a value from "+minValue+" to "+maxValue+" ...\n "
       alert(alertsay);
       return (false);
       }
    }

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Posts
    117
    Thanks
    0
    Thanked 17 Times in 17 Posts
    There are some logical errors in the code

    Code:
    var allValid = true;
    for (j = 0;  j < checkOK.length;  j++)
    {
       if (ch != checkOK.charAt(j)){allValid =false; break;}
    }
    will be better.

    Anyways, there are easier number checks in JS. See:
    http://stackoverflow.com/questions/1...ript-isnumeric

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, but I cannot get your modification to work. Can you post the entire script with the changes already included so I can test it -- without possibly messing it up myself? Thanks.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    Quote Originally Posted by owkaye View Post
    Thanks, but I cannot get your modification to work. Can you post the entire script with the changes already included so I can test it -- without possibly messing it up myself? Thanks.
    Show your code (including HTML portion) with or without modifications.

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was frustrated because I couldn't get it to work after trying the better code that prasanthmj posted earlier, so I kept looking online until I found a far better solution than the one I started with.

    I'm using LiveValidation now. It seems to do all kinds of things even better than I had hoped.


  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,344
    Thanks
    11
    Thanked 589 Times in 570 Posts
    going forward: <input required />
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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