...

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



owkaye
07-14-2011, 02:31 AM
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.


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);
}
}

prasanthmj
07-14-2011, 04:58 AM
There are some logical errors in the 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/18082/validate-numbers-in-javascript-isnumeric

owkaye
07-14-2011, 05:46 AM
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.

jmrker
07-14-2011, 07:51 PM
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.:eek:

owkaye
07-15-2011, 12:00 AM
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.

:)

rnd me
07-15-2011, 03:30 AM
going forward: <input required />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum