View Full Version : Insert Negative sign when needed if given value is positive

03-12-2008, 07:54 AM
I need to validate a textfield by giving -ve/+ve values and the textfield it takes maxLength = "3"

for Ex: if i give "-123" the textfield is accepting but
if i give as 123 and later if i give "-" before 1 it is notaccepting i need to delete one character and
add the - but like that it shouldn't happen

even if i give positive value and later i want that field to be negative sign i should have the
ability to place - sign

can anybody help me in this regard.

03-13-2008, 03:19 PM
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sign of a limited length number</title>
function getNumber(){
var d = document;
var signObj = d.getElementById("sigIn");
var sign = signObj.options[signObj.selectedIndex].value;
var num = d.getElementById("numIn").value;
var Num = eval(sign+num);
return true;
<select id="sigIn">
<option value="+" selected="selected">+</option>
<option value="-">-</option>
<input id="numIn" type="text" maxlength=3 size=3 value="123">
<input id="butAlert" type="button" value="Check" onclick="getNumber()">

Simply add a select button with choice of signs.
If this is not what you want, like if you want a pure JS method, just clarify what you want.

03-14-2008, 12:18 PM
That is not the case actually , i need to validate the given value including
- value or + value in the textfield only

and the textfield maxlength = "3"

if i give the value in the textfield as "-123" i.e., onKeyPress(...) only
the textfield should accept or if i give as "123" it should accept.