Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    184
    Thanks
    140
    Thanked 0 Times in 0 Posts

    String and number question

    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.

    Code:
    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();"
    >

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    That is very old code. Use this instead:-

    Code:
    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?"
    Last edited by Philip M; 06-29-2012 at 05:37 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    jason_kelly (07-05-2012)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    another way to skin poor kitty …

    Code:
    <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>
    Last edited by DaveyErwin; 06-29-2012 at 06:27 PM.

  • Users who have thanked DaveyErwin for this post:

    jason_kelly (07-05-2012)


  •  

    Posting Permissions

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