...

View Full Version : can someone MOD this script for me?



wfcentral
05-05-2004, 05:24 PM
I have a script that checks for numeric values in a form field. The code is slightly over my head, but I want it to also accept the "-" symbol so that users can enter phone number or social security number but NOT letters... This is part of a much larger project so I can't use any other solutions... I need to MOD this exact code. Probably just changing one word or line, but I'm not having any luck... anyway, here is the code...

/* The isNumeric function validates a string to determine whether or not it contains a numeric value.
Parameters: lstr - Contains string value to validate
Returns: true/false */
function isNumeric(lstr)
{
lstr = rtrim(lstr);
if (lstr != "")
{
//declare local variables
var strlen, curptr, setptr, chk, inloop, decflag, minusflag, iserror;
iserror = false;
decflag = false;
minusflag = false;
strlen = lstr.length;
curptr = 0;
chk;
// first check for space - .
inloop = true;
for (curptr = 0; curptr < strlen; curptr++)
{
chk = lstr.charAt(curptr);
if (chk >= "0" && chk <= "9")
{
break;
}

if (chk == "-")
{
minusflag = true;
break;
}
if (chk == ".")
{
decflag = true;
break;
}
if (chk != " ")
{
return false;
}
}

if (curptr >= strlen-1)
{
if (decflag || minusflag || chk == " ")
{
return false;
}
else
{
return true;
}
}
setptr = curptr + 1;
for (curptr = setptr; curptr < strlen; curptr++)
{
chk = lstr.charAt(curptr);
if (chk < "0" || chk > "9")
{
if (chk != ".")
{
return false;
}
else
{
if (decflag)
{
return false;
}
else
{
decflag = true;
}
}
}
}
return true;
}
return false;
}

A1ien51
05-05-2004, 06:36 PM
that is some ugly code that a one lined regualr expression could do
(In the future use code tags so it is easier to read)


Try this:

if (decflag || minusflag || chk == " ")

change to

if (decflag || chk == " ")

Eric

Philip M
05-05-2004, 06:54 PM
An awful lot of code which a regular expression would eat for breakfast.

function isNumeric(lstr) {
if (/[^0-9\s\-]/gi.test(lstr.value)) {
// test fails
}
}


In other words, the test fails if anything other than the digits 0-9, a space or a hyphen are entered into the field.

"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

wfcentral
05-05-2004, 07:04 PM
Alien 51 - tried it... didn't work...bummer...


Phillip - tried yours too... looked amazing. It allows letters to get through though??

Philip M
05-05-2004, 07:16 PM
No, if letters or characters such as $ % & ( ) are entered the test fails.

If a character within the string to be tested is NOT 0-9 and NOT a space and NOT a hyphen the test fails. OK?

wfcentral
05-05-2004, 07:20 PM
Phillip -

I replaced the entire function that I was using with your new example. I then went to the form and tested it by putting in "444-55-pppp" and it went through just fine... (meaning it did NOT tell me that I entered non-numeric data)

Was I supposed to use your lines of code AND some code from my original post?

sorry,
Robert

Philip M
05-05-2004, 08:16 PM
Sorry, there were a couple of typos.

This works:-

<SCRIPT = JavaScript>
function isNumeric(lstr) {
alert (lstr.value);
if (/[^0-9\s\-]/gi.test(lstr.value)) {
alert ("Test failed");
return false;
}
alert ("test passed");
return true;
}
</SCRIPT>

wfcentral
05-05-2004, 08:35 PM
Phillip,

that almost works... the test always fails now though... I can't get it to accept anything...

Robert

Philip M
05-05-2004, 08:53 PM
Robert -

It is working for me.

I entered 123 4-5 and it passed
I entered 123ab and it failed.

What does alert(lstr.value) show you (i.e. is the passed value correct?)


Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

wfcentral
05-05-2004, 09:02 PM
it says "undefined"

Philip M
05-05-2004, 09:10 PM
Here is the whole thing I have been testing on:-

<HTML>
<HEAD>
</HEAD>

<BODY>

<tr>
<td><font face="Arial"><em><b>Enter some text here</b></em></font></td>
<td><input type="text" size="25" maxlength="25" name="testtext" onblur="isNumeric(this)"> </font></td></tr>

<SCRIPT = JavaScript>
function isNumeric(lstr) {
alert (lstr.value);
if (/[^0-9\s\-]/gi.test(lstr.value)) {
alert ("Test failed");
return false;
}
alert ("test passed");
return true;
}

</SCRIPT>

</BODY>
</HTML>

If lstr.value is undefined it must be that the value is not being passed.

Check your code for onblur="isNumeric(this)" (including spelling, case etc. as JavaScript is case sensitive and isnumeric is not the same as isNumeric.

Philip M
05-05-2004, 09:25 PM
It occurs to me that a blank textbox will also pass, which is probably not what you want.

So here is a mod to overcome that:-

<SCRIPT = JavaScript>
function isNumeric(lstr) {
alert (lstr.value);
if (/[^0-9\s\-]/gi.test(lstr.value) || (lstr.value.length==0)) {
alert ("Test failed - invalid characters");
return false;
}
}
alert ("Test passed");
return true;
}

</SCRIPT>

You can of course then specify a minimum length for lstr (say 3 digits or whatever) by altering == 0 to <4 or whatever.

wfcentral
05-05-2004, 09:28 PM
Phillip - THANKS FOR ALL YOUR HELP

I found the reference you were talking about in the old code and made some slight modifications to your code - all works GREAT now!



function isNumeric(lstr) {
lstr = rtrim(lstr);
alert (lstr);
if (/[^0-9\s\-]/gi.test(lstr)) {
alert ("Test failed");
return false;
}
alert ("test passed");
return true;
}


- Robert

Philip M
05-05-2004, 09:35 PM
Great Robert,

But have a look at my post a few seconds before yours for an improvement!

Glad to have helped. This is an excellent example of how a simple regular expression can replace great chunks of ugly code.

wfcentral
05-05-2004, 09:40 PM
hey Phillip,

before you head off...

what would I modify if I want people to be able to enter ' or "

such as how tall are you? 5' 4"

Thanks,
Robert

wfcentral
05-05-2004, 10:37 PM
Phillip,

I tried your modified code (to not allow blank fields) but for some reason it doesn't work - I don't need that feature though, this function is one of a dozen that came with a formcreator program - there is another function already checking for empty fields (when marked as required).

Thanks,
Robert

Philip M
05-06-2004, 07:42 AM
Just add the permitted characters to the regex:-

if (/[^0-9\s\-\"\']/gi.test(lstr)) {

The backslashes are escape characters to show that they are
to be interpreted literally, and cancel their regex special meanings. ^ means NOT one of the characters within the square brackets. \s means space.

If you wanted to allow literal letters such as "x" and "s" in the text field, then you would make it:-

if (/[^0-9\s\-\"\'xs]/gi.test(lstr)) {

Philip M
05-06-2004, 07:51 AM
It works for me! Not sure why it does not for you.

Not sure what rtrim does exactly.

Try:-

if (/[^0-9\s\-]/gi.test(lstr.value) || (lstr=="")) {




Phillip,

I tried your modified code (to not allow blank fields) but for some reason it doesn't work - I don't need that feature though, this function is one of a dozen that came with a formcreator program - there is another function already checking for empty fields (when marked as required).

Thanks,
Robert

wfcentral
05-06-2004, 03:55 PM
modifications to add the ' and " characters work great...

I'm on to other things now... thanks for all your help!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum