PDA

View Full Version : RegExp not working.



qwerty_face
01-24-2011, 03:46 AM
Hi all,

I am having some troule with searching a string taken from a text field (a name textfield, it's basically a form and the script is validating it).

I've been on the W3C schools website and I cannot get it to work, even with the "try it" feature.

Please help.




function nameValidator(entry, msg)
{
var regExp = new RegExp("[a-z ]", "g");
var s_regExp = new RegExp("[ ]");
var tub = entry.value;
var newLength = parseInt(tub.length);
var i = 0;
var space = 0;

if (tub == "" || newLength > 20)
{
alert(msg + "entry must be > 1 and < 20 characters only!");
return false;
}
else if (tub.match(regExp))
{
for (i=0; i < newLength; i++)
{
if (tub.substr(i,1) == "")
{
space += 1;
}
}
if (space < 1)
{
alert(popup + "you must enter a space.")
return false;
}
if (space > 1)
{
alert(msg + "you cannot enter a space more than once.");
return false;
}
if (tub.substr(0,1) == s_regExp ||
tub.substr(newLength,1) == s_regExp)
{
alert(popup + "the first and last characters cannot be whitespace.");
return false;
}
}
return true;
}


1. Specifically, it will not correctly evaluate the statement where I've used the s_regExp variable nor will it detect white space at the space counter so it never increases (which the rest of the code relies on!).
2. What am I doing wrong. W3C states just also to use var newvar = new RegExp("\s") but that doesn't work, and I have even tried var newvar = new RegExp("^([a-z ])*$", "g") and that's useless too.

mrhoo
01-24-2011, 04:05 AM
new RegExp("\s") should escape the escape-

new RegExp("\\s")

qwerty_face
01-24-2011, 04:52 AM
Nope, doesn't work.

In Firebug, it detects it as /\s/ which is the same as exactly typing that without the new RegExp declaration - that also does not work

It really is a load of rollocks this. Getting right on my knackers.

Krupski
01-24-2011, 05:16 AM
Nope, doesn't work.

In Firebug, it detects it as /\s/ which is the same as exactly typing that without the new RegExp declaration - that also does not work

It really is a load of rollocks this. Getting right on my knackers.

Check out this site: http://regexpal.com

It's positively awesome. You can enter a regex in one "window" and see the live results in the other. There's also a built in "cheat-sheet" that pops out on mouseover, and you can pin it open if you need to.

-- Roger

Old Pedant
01-24-2011, 05:16 AM
This doesn't make sense:


alert(msg + "entry must be > 1 and < 20 characters only!");

If you are requiring a space in the middle and no space at each end, then the minimum length is obviously 3 characters. And pardon me, but 20 characters is too short for my full name, just to pick an example.

And are you *really* willing to accept the name "x z" as a legitimate name? As things stand now, you are.

And what about the poor guy named "John O'Brien" or the woman named "Katherine Zeta-Jones"??? You don't allow the apostrophe or dash characters.

In short, this entire process seems to be disallowing legitimate names while allow clearly fake ones.

Maybe it would help if you specified *exactly* what you are willing to accept as a good name?

Old Pedant
01-24-2011, 05:17 AM
Oh...and by the way, instead of disallowing leading or trailing spaces, simply trim them off for the user. Much more user-friendly.

qwerty_face
01-24-2011, 05:53 AM
because for my purposes in this case:

1. I require only lower case letters a to z , "-" (dash) and " " (space), 20 character length
c) the "-" and " " must be entered
d) the "-" or the " " letter must not be either the first or the last letter entered,
e) "-" must not be the immediate neighbour " ",
f) "-" or " " must not be the immediate neighbour of itself

And while your busy picking holes in my work, well be supportive - I am a complete novice. Plus, I had already noticed the flaws but these are my requirements.

Old Pedant
01-24-2011, 07:22 AM
Okay...but you do allow multiple dash and multiple space??

That is, the following *IS* legal, according to what you wrote:


a-b c-d e-f g-h

Yes?

Old Pedant
01-24-2011, 07:34 AM
Assuming that "a-b c-d e-f g-h" is legal, then:


function funkyValidator( fld, msg )
{
var tub = fld.value.replace(/^\s+/,"").replace(/\s+$/,""); // optional replace()s trim ends
fld.value = tub; // put the trimmed value back (optional)

var test1 = /[a-z\s\-]{3,20}; // or 5,20 if both space and dash must appear
var test2 = /(^\-|\-\s|\s\-|\s\s|\-\-|\-$)/; // illegal combos

if ( test1.test( tub ) && ! test2.test( tub ) ) return true;
alert( msg + " does not fit the required pattern" );
return false;
}


Untested, but feels right. If you confirm/deny my supposition about legit patterns, I'll maybe actually test it or test a revision.

Philip M
01-24-2011, 09:28 AM
<script type = "text/javascript">

function funkyValidator(fld, msg) {
var tub = fld.value.replace(/^\s+/,"").replace(/\s+$/,""); // optional replace()s trim ends
fld.value = tub; // put the trimmed value back (optional)

// add code here to test for just one space and one hyphen if required

var test1 = /^[a-z\s\-]{5,20}$/; // only lower-case a-z space hyphen allowed (not A-Z)
var test2 = /\s/; // match a space
var test3 = /-/; // match a hyphen
var test4 = /(^\-|\-\s|\s\-|\s\s|\-\-|\-$)/; // illegal combos

if (test1.test(tub) && test2.test(tub) && test3.test(tub) && !test4.test(tub)) {
alert ("The pattern is valid");
return true
}
else {
alert (msg + " does not fit the required pattern" );
return false;
}

}
</script>

If only one space and one hyphen are allowed, then add this code to the above where indicated:-


var spc = /\s/g;
var hyph = /-/g;
if ((tub.match(spc).length > 1) || (tub.match(hyph).length > 1)) {
alert ("More than one space or hyphen - not allowed");
return false;
}

You can test your regular expressions at: http://www.claughton.clara.net/regextester.html

Quizmaster: In US history, during the Great Depression, parts of the midwest afflicted by drought and high winds became known as the what bowl?
Contestant: Super