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 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts

    test a string then return

    i need to make a sub routine to take the characters typed and make sure the first 2 numbers are 07
    this is what i have to make sure its only numeric

    Code:
    <SCRIPT language=Javascript>
          function isNumberKey(evt)
          {
             var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
             return true;
          }
    
       </SCRIPT>
    the numbers would eventualy be 11 numbers long but i need the first 2 typed to be 07 and nothing else.
    this function is called onkeypress.
    any clever ideas

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You cannot do that test onkeypress as naturally the pattern does not match until it is completed - only onblur. You could use your existing code onkeyup to check that the user enters only digits. You could also pre-populate the textbox with the digits 07.

    Code:
    <script type = "text/javascript">
    
    var numstr = "07123456789";   // must be in quotes - a string
    if (!/^(07)/.test(numstr) || (/[^0-9]/.test(numstr))  || (numstr.length !=11 )) {
    alert ("Invalid entry - must be 11 digits starting with 07");
    }
    else {
    alert ("OK");  
    }
     
    </script>
    Note that a value starting with 0 is a string and not a number, unless you wish it to interpreted as octal.
    Code:
    var x = 0723;
    alert (x);  // 467 decimal

    Don't wanna kill myself. I've been there and done that before, and it just makes me miserable. - Interviewee, Sky Living
    Last edited by Philip M; 05-27-2012 at 10:30 AM.

    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.

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You cannot do that test onkeypress as naturally the pattern does not match until it is completed - only onblur. You could use your existing code onkeyup to check that the user enters only digits. You could also pre-populate the textbox with the digits 07.

    Code:
    <script type = "text/javascript">
    
    var numstr = "07123456789";   // must be in quotes - a string
    if (!/^(07)/.test(numstr) || (/[^0-9]/.test(numstr))  || (numstr.length !=11 )) {
    alert ("Invalid entry - must be 11 digits starting with 07");
    }
    else {
    alert ("OK");  
    }
     
    </script>
    Note that a value starting with 0 is a string and not a number, unless you wish it to interpreted as octal.
    Code:
    var x = 0723;
    alert (x);  // 467 decimal

    Don't wanna kill myself. I've been there and done that before, and it just makes me miserable. - Interviewee, Sky Living
    sounds like a good code.. i shall try that thankyou

  • #4
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    that code worked perfectly thankyou

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Yes, as I say in my signature:-

    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.

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i have a related problem now..
    i have about 20,000 users using my site but some have not refreshed there page to have the updated script. so they are still getting through the filter.
    how would you go about forcing them to get the fresh page.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You can't, except show a message requesting them to do so.

    Code:
    <p><input type = "button" value = "Click here to refresh this page and see the latest version" style="width:30em" onclick = "javascript:location.reload(true)">
    Or rename the page.

    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.

  • #8
    New Coder
    Join Date
    Nov 2011
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    what do you mean by rename the page
    rename the index.html to index1.html and get my domain to point to that..
    or rename the title

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by gerble1000 View Post
    what do you mean by rename the page
    rename the index.html to index1.html and get my domain to point to that..
    or rename the title
    If the page is index.html then you cannot readily rename it. You could have renamed another page.

    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.


  •  

    Posting Permissions

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