Reputation: 1411
I need to validate a number entered by a user in a textbox using JavaScript. The user should not be able to enter 0 as the first digit like 09, 08 etc, wherein he should be allowed to enter 90 , 9.07 etc..
Please help me.. I tried to use the below function but it disallows zero completely:
function AllowNonZeroIntegers(val)
{
if ((val > 48 && val < 58) || ((val > 96 && val < 106)) || val == 46 || val == 8 || val == 127 || val == 189 || val == 109 || val == 45 || val == 9)
return true;
return false;
}
on
Texbox1.Attributes.Add("onkeypress", "return AllowNonZeroIntegers(event.keyCode)");
Texbox1.Attributes.Add("onkeydown", "return AllowNonZeroIntegers(event.keyCode)");
Upvotes: 1
Views: 2458
Reputation: 76218
Ty this:
Texbox1.Attributes.Add("onkeydown", "return AllowNonZeroIntegers(event)");
function AllowNonZeroIntegers(e) {
var val = e.keyCode;
var target = event.target ? event.target : event.srcElement;
if(target.value.length == 0 && val == 48) {
return false;
}
else if ((val >= 48 && val < 58) || ((val > 96 && val < 106)) || val == 46 || val == 8 || val == 127 || val == 189 || val == 109 || val == 45 || val == 9) {
return true;
}
else {
return false;
}
}
Upvotes: 0
Reputation: 8767
Use charAt()
to control the placement of X at Y:
function checkFirst(str){
if(str.charAt(0) == "0"){
alert("Please enter a valid number other than '0'");
}
}
Upvotes: 3