Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate numerical value 2

Status
Not open for further replies.

omoo

Programmer
May 30, 2005
87
US
Hi, I have the following java function that validate the form for numerical values:

Code:
function validate_numeric(field, alerttxt)
{
	with (field)
	{
		var strValidChars = "0123456789";
		var strChar;
		var blnResult = true;

   		for (i = 0; i < value.length && blnResult == true; i++)
      		{
      			strChar = value.charAt(i);
      			if (strValidChars.indexOf(strChar) == -1)
         			{alert(alerttxt);blnResult = false}
      		}
      		return blnResult;
      	}
}

and the following form:

Code:
function validate_form4(thisform)
{
	with (thisform)
	{					
		if (validate_numeric(NO_OF_TESTERS,"Please enter a numerical value")== false)
			{NO_OF_TESTERS.focus();return false}

	}
}

I could not find the reason why this is not working. Can anyone help?
 
There's an easier way to validate a number using the native methods parseInt and isNaN (Is Not A Number):
Code:
function isNumeric(input) 
{
    return (!isNaN(parseInt(input));
}

and you never need [tt]if(x==true)[/tt] or [tt]if(x==false)[/tt]. Instead you should use [tt]if(x)[/tt] or [tt]if(!x)[/tt].
Code:
function validate_form4(thisform)
{
    with (thisform)
    {                    
        if (!isNumeric(NO_OF_TESTERS)) {
            alert("Please enter a numerical value")
            NO_OF_TESTERS.focus();
            return false;
        }
    }
}

---
Marcus
better questions get better answers - faq581-3339
accessible web design - zioncore.com
 
manarth said:
you never need if(x==true) or if(x==false). Instead you should use if(x) or if(!x).

I think "can", rather than "should" would be more appropriate - people should feel free to use whichever notation they choose.

Of course, it's always good to me made aware of the alternatives ;-)

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Just a note:
[tt]
var input="1abc"
var n=parseInt(input)); //result in n=1
var b=isNaN(n); //result in true
[/tt]
The isNumeric() using these will be defective.

The simplest is to use regular express to validate decimal string.
 
Here are some functions I use to cover a variety of possibilities.
The any_real_number_with_set_number_Decimals function accepts two additional parameters to set the minumum and maximum number of decimal places.

Code:
function whole_positive_number(value) {
  var re = /^\d+$/;
  return re.test(value);
}

function any_whole_number(value) {
  var re = /^-?\d+$/;
  return re.test(value);
}

function any_real_number_with_decimal(value) {
  var re = /^-?\d+\.\d+$/;
  return re.test(value);
}

function any_real_number_with_or_without_decimal(value) {
  var re = /^-?\d+(\.\d+)?$/;
  return re.test(value);
}

function any_real_number_with_set_number_decimals(value,min,max) { //Accepts number without decimal but if decimal exists it must have at least the min and at most the max.
  var re = new RegExp("^-?\\d+(\\.\\d{" + min + "," + max + "})?$");
  return re.test(value);
}

function valid_integer(value) {
  var re = /(^-?\d\d*$)/;
  return re.test(value);
}

Stamp out, eliminate and abolish redundancy!
 
This is what I use for number validation, works in IE and FF and NS.

Code:
<script>
	function checknumber(data) {
	var checkOK = "()-0123456789. ";
	var checkStr = data.value;
	var allValid = true;
	var allNum = "";
	
	  for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.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) {
		  alert("Please enter a valid phone number");
		  data.value = "";
		return (false);
	  }
	}
</script>

This portion of the code var checkOK = "()-0123456789. ";, allows you to specify what chars can be passed. In my case, I am allowing all numeric chars, as well as the parenthesis, dash and white space. (In case its not evident, this is my phone vlaidation script).

To call this function, you do: onChange="checknumber(this);", you can use any even handler, doesn't have to be onChange.

[sub]
____________________________________
Just Imagine.
[sub]
 
Hi manarth,

Your code is good for checking numbers but I realise I need to allow decimals too. This validation will not allow decimals. Can anyone help?
 
Aren't other persons' posting not helping you enough?!
 
Oh, now I saw all the latest posts. Thanks all for the help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top