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

validate form text box - percent increase

Status
Not open for further replies.

greatfalls

Technical User
Jul 1, 2001
21
0
0
CA
I have the following functin in a form. I need to make it so that any percent greater then 10.00 (10 percent) doesn't get submited, and a alert apears instead.

Any help would be appreciated, Thanks

function chkIncrease(fld) {
var str = fld.value.replace(/\s/g,'');
if (str.length==0) return true;
if (str.length <10) return true;
if (!/^[0-9]{1,2}\.?[0-9]?$/.test(str)) {
window.alert(&quot;This must be in 99.9 format.&quot;);
fld.focus();
fld.select();
return false;
}
 
It shouldn't be str.length. str.length gets the length of the textfield instead of the value. Try if(str == 0) and if(str <10) alone should work.

Regards,
Leon
 
I am presuming that the user enters a number in the text field to represent the percent value. I would have a function like:

function chkIncrease(fld) {

var str = fld.value;

// Make sure they entered a valid number
if (isNaN(str)) {
alert(&quot;This must be in 99.9 format.&quot;);
fld.focus();
fld.select();
return false;
}

// Convert the string to a float variable
var num = parseFloat(str);

// Is the float greater than 10
if (num > 10) return false;
else return true;

} Mise Le Meas,

Mighty :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top