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

Limit the Input field to Have only 1 + sign or - sign in it.

Status
Not open for further replies.

wrighterb

Programmer
Feb 20, 2007
80
US
I want to Limit the Input field to Have only 1 + sign or - sign in it.

Any idea?
 
You could attach an onkeyup event to the input that triggers a function every time a key is released in the input. The function could be just a regular expression that replaces the value of the input with any unwanted extra + characters removed.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Without being more specific on the regular expression, I'm not sure how to do it using one.

You can loop through each character of the input string and check the character with a charAt function. Keep track if you have one or more + or - signs, if more than one, alert the user to remove them.

[monkey][snake] <.
 
I have this as my onkeyup function. So you say a loop to it?


function clearChar() {
document.adjustment.adjustAmount.value=filterNum(document.adjustment.adjustAmount.value)
function filterNum(str) {
re = /[^0-9.+-}]/g
return str.replace(re, "");
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top