...

View Full Version : test a string then return



gerble1000
05-27-2012, 09:47 AM
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


<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 :)

Philip M
05-27-2012, 10:01 AM
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.


<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.

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

gerble1000
05-27-2012, 10:43 AM
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.


<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.

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

gerble1000
05-27-2012, 10:49 AM
that code worked perfectly thankyou :)

Philip M
05-27-2012, 10:58 AM
Yes, as I say in my signature:-

gerble1000
05-27-2012, 12:51 PM
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.

Philip M
05-27-2012, 03:11 PM
You can't, except show a message requesting them to do so.


<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.

gerble1000
05-27-2012, 03:47 PM
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

Philip M
05-27-2012, 06:52 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum