...

View Full Version : String and number question



jason_kelly
06-29-2012, 03:42 PM
Hello,

I was wondering if it was possible to get the help of an expert here to help me with the following.

I have the following function below that does real time number checking, however, id like to modify the coding such that it takes into account that the the number entered into the textbox cannot start off as a 0, if its a zero, the function will automatically enter is as a "1"

I was thinking that regex might be really usefull here, but I dont even know where to start.



function onlyNumbers(evt) {
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false }
return true;
}

<input
style="padding-left: 4px; font-family: arial; font-size: 9pt; width: 35px; height: 15px; border: none"
type="text"
id="row"
maxlength="4"
onkeypress= "return onlyNumbers();"
>

Philip M
06-29-2012, 05:31 PM
That is very old code. Use this instead:-


Numbers Only In Here <input type = "text" id = "mytext" onkeyup = "checkNumsOnly(this)">

<script type = "text/javascript">

function checkNumsOnly(which) {
var val = which.value;
var len = val.length;
if (/\D/.test(val)) {
val = val.substring(0,len-1);
}
val = val.replace(/^0/,"1"); // replace leading zero by 1

which.value = val;
}

</script>

To block a leading zero change
val = val.replace(/^0/,"1"); // replace leading zero by 1
to
val = val.replace(/^0/,""); // no leading zero allowed


Tourist in hotel in Jasper, Canada: "Do we need to convert our Canadian currency into British pounds when we go on to British Columbia?"

DaveyErwin
06-29-2012, 05:40 PM
another way to skin poor kitty


<doctype html>
<html>
<head><title></title>
</head>
<body>
<input
style="padding-left: 4px; font-family: arial; font-size: 9pt; width: 35px; height: 15px; border: none"
type="text"
id="row"
maxlength="4"
onkeyup="if(isNaN(value=parseInt(value)))value='';if(value=='0')value=1;"
></body><html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum