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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching control for negitive (-) sign. 1

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

I have an input textbox the user enters numbers into. I only want the user to enter positive numbers. I wrote this function that checks the contents of the control - searching for letters or "-" negitive sign. It picks up the letters fine. But does not pick up the "-" sign. My code is below:

Being called from the input textbox onBlur method.
Code:
function chkMinAmount(frmName) {
	var tempNum = document.forms[frmName].fromAmt.value;
	
	if (tempNum.length != 0) {
		if (amountCheck(frmName,tempNum)) {
			alert("Enter positive numbers only.");
			document.forms[frmName].fromAmt.value = "";
			document.forms[frmName].fromAmt.focus();
		}else {
			var newValue = formatNumber(tempNum);
			document.forms[frmName].fromAmt.value = newValue;
			document.forms[frmName].amountRange.checked = true;
		}
	}	
}

Called from the ckMinAmount function.
Code:
function amountCheck(frmName,amountIn) {

	var amt = amountIn;
	
	if (!amt) return false;
	
	var iChars = "-AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
	
	for (var i = 0; i < amt.length; i++) {
		if (iChars.indexOf(amt.charAt(i)) == -1)
			return false;		
	}
	return true;
}

Any help would be most appreciated.

TIA,
Tim
 
Whoops, did it wrong:
Code:
function amountCheck(frmName,amountIn) {
  return /(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)/.test(amountIn);
}

Adam

Whatever I feel like I wanna do, gosh!
 
or this change:

Code:
function chkMinAmount(frmName) {
    var tempNum = document.forms[frmName].fromAmt.value;
    
    if (tempNum.length != 0) {
        if (amountCheck(frmName,tempNum)) {
            var newValue = formatNumber(tempNum);
            document.forms[frmName].fromAmt.value = newValue;
            document.forms[frmName].amountRange.checked = true;
        }else {
            alert("Enter positive numbers only.");
            document.forms[frmName].fromAmt.value = "";
            document.forms[frmName].fromAmt.focus();
        }
    }    
}

Code:
function amountCheck(frmName,amountIn) {

    var amt = amountIn;
    if (!amt) return false;
    
    var iChars = "-AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
    
    for (var i = 0; i < amt.length; i++) {
        if (iChars.indexOf(amt.charAt(i)) > -1)
            return false;        
    }
    return true;
}

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks Adam. Will try your suggestion.
 
That's if you want a decimal. If you want an integer, you could use this:
Code:
function amountCheck(frmName,amountIn) {
  return /^\d+$/.test(amountIn);
}

Adam

Whatever I feel like I wanna do, gosh!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top