Results 1 to 3 of 3

Thread: Insert Negative sign when needed if given value is positive

  1. #1
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Insert Negative sign when needed if given value is positive

    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.

  2. #2
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default This works

    Code:
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    		<title>Sign of a limited length number</title>
    		<script>
    			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);
    				alert(Num);
    				return true;
    			}
    		</script>
    	</head>
    	<body>
    		<select id="sigIn">
    			<option value="+" selected="selected">+</option>
    			<option value="-">-</option>
    		</select>
    		<input id="numIn" type="text" maxlength=3 size=3 value="123">
    		<input id="butAlert" type="button" value="Check" onclick="getNumber()">
    	</body>
    </html>
    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.

  3. #3
    Join Date
    Mar 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default validating the textfield with -ve/+ve value by maxlength="3"

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •