[SOLVED] playing with Perl Compatible Regex in field validation

This is a discussion on "[SOLVED] playing with Perl Compatible Regex in field validation" within the JavaScript Forum section. This forum, and the thread "[SOLVED] playing with Perl Compatible Regex in field validation are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 25th, 2007, 13:30
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb [SOLVED] playing with Perl Compatible Regex in field validation

The field validation never ends!

So at the moment the code currently rips any non numerical character and replaces it with literally nothing. It then checks to see if the entry is a number and if not raises an alert. then with if else commands the javascript checks to see that the number is no greater or less than 11 digits long.

The code looks like this:

Code: Select all
    document.callmeback_form.telephone.value = document.callmeback_form.telephone.value.replace(/\D/g, "");
    
    if (document.callmeback_form.telephone.value == "" || isNaN(parseInt(document.callmeback_form.telephone.value)))
    {
    alert ( "Please enter your number (inc. area code)." );
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border= "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width= "132px";
    document.callmeback_form.telephone.style.height= "15px";
    return false;
    } 
    else if (document.callmeback_form.telephone.value.length < 11)
    {
    alert ("The number you have entered is too short, please check and try again");
    return false;
    }
    else if (document.callmeback_form.telephone.value.length > 11)
    {
    alert ("The number you have entered is too long, please check and try again");
    return false;
    }
What i need it to do on top of this now is:

1.let the '+' character through when replacing characters.

2.If the '+' is not the first character in the sequence after this then raise an alert.

3.If it is the first character and is followed by '44' then return true if not then raise an alert.

4.Allow a 14 digit number to return true only if it starts with '0044'

Any ideas??

Thanks again for all of the help so far. If I notice that anyone has posted something I can help with I will definetly pitch in. Dan.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 25th, 2007, 15:13
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

anyone??? Im still stuck on this.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 25th, 2007, 19:00
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

Here's a little test page I wrote. See if you can run with this regex:
HTML: Select all
<html>
<body>
<script type="text/javascript">
function regexTest() {
    if(document.getElementById('blub').value.match(/^(\+44[0-9]{9}|0044[0-9]{10})$/)) {
        alert('match');
    } else {
        alert('no match');
    }
}
</script>
<form onsubmit="return false;">
<input type="text" id="blub" />
<input type="button" onclick="regexTest();" value="check match" />
</form>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 26th, 2007, 10:21
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
phone number field validation

Im building a phone number field validation. Its getting there but im running into problems.
Please take into account im builiding it only for the uk. so +44 and +0044 interntaional call numbers apply!

Ok so far my code is at this state..
Code: Select all
var temp = document.callmeback_form.telephone.value;
    
    if (temp=="") 
    {
    alert("Please enter your number (inc. area code).");
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width = "132px";
    document.callmeback_form.telephone.style.height = "15px";
    return false;
    }
    
    else if (temp.length == 14 && temp.subStr(0,3)!="0044") 
    {
    alert("Invalid number.");
    return false;
    }
        
    else if (temp.length == 13 && temp.subStr(0,2)!="+44") 
    {
    alert("Invalid number.");
    return false;
    }
    
    else if (temp.length == 11 && temp.subStr(0)!="0") 
    {
    alert("Invalid number.");
    return false;
    }

    else if (temp.length < 11) 
    {
    alert("The number you have entered is too short, please check and try again.");
    return false;
    }
    
    else if (temp.length > 11) 
    {
    alert("The number you have entered is too long, please check and try again.");
    return false;
At the moment the field works for.

1. a alert if you enter nothing

2. lets through 0044**********

3. lets through +44**********

4. lets through 0**********

5. Throws up an error if the number is under 11 characters (if it applies to rule number 4)

6. Throws up an alert if the number is over 11 characters (if it applies to rule number 4)



But. The strange thing is, that it will:

accept any number for the 0044 rule eg.3546

accept any number for the +44 rule eg.+55

accept any character

accept any number for the 0 rule eg 1


I cant see why. Can anyone else???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 26th, 2007, 10:37
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

Have you tried c010depunk's regex from above? It will do a validation on all you need to check at once.


The code would look something like

Code: Select all
var temp = document.callmeback_form.telephone.value;
    
    if (!temp.match(/^(\+44[0-9]{9}|0044[0-9]{10})$/)) 
    {
    alert("Enter a valid UK Phone number including area code");
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width = "132px";
    document.callmeback_form.telephone.style.height = "15px";
    return false;
    }
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Oct 26th, 2007 at 10:41.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 26th, 2007, 10:39
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

Amen, bro. Regex RockZ!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 26th, 2007, 10:44
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

yes that would work. But i need seperate alert boxes to make the user aware of where they have made the mistake. You would of thought they could just figure that out themselves. Stupid users!!!

I think my problems boil down to this chunk of code...
Code: Select all
else if (temp.length == 14 && temp.subStr(0,3)!="0044") 
	{
    alert("Invalid number.");
    return false;
    }
		
	else if (temp.length == 13 && temp.subStr(0,2)!="+44") 
	{
    alert("Invalid number.");
    return false;
    }
	
	else if (temp.length == 11 && temp.subStr(0)!="0") 
	{
    alert("Invalid number.");
    return false;
    }
they return true when needed. Yet refuse to return false and create the alert. I just cant see why!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 26th, 2007, 10:53
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

Don't capitalise the second 's' in subStr.

Use temp.substr(0,2) etc....

PS. I would place an example number next to the field then fail the submission if it didn't match the regex... The user can see the example if they are wondering how to enter it.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 26th, 2007, 10:58
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

You know Rakuli you were right with the regex stuff i think now. But I have a question about the way that works...

Code: Select all
if (!temp.match(/^(\+44[0-9]{9}|0044[0-9]{10})$/))
the [0-9] part - is this the characters allowed to be entered??
the {9} part - is this the number of characters allowed???

this does seem like the best way to do it as I can just add...
Code: Select all
    else if (temp.length < 11) 
	{
    alert("The number you have entered is too short, please check and try again.");
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width = "132px";
    document.callmeback_form.telephone.style.height = "15px";
	return false;
    }
	
    else if (temp.length > 11) 
	{
    alert("The number you have entered is too long, please check and try again.");
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width = "132px";
    document.callmeback_form.telephone.style.height = "15px";
	return false;
    }
after it.
thanks dan.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 26th, 2007, 11:03
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

Hi eon201,

Thanks go to c010depunkk for the regex, I just popped it in your code ..

You know you wouldn't even need the second part of the code as the match checks that the post is the right length...

But I guess if you *need* the user to see that they can't remember their number it can't hurt.. lol
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 26th, 2007, 11:16
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

Im sorry about this. You guys must be sick of me now.

so currently the code works and looks like this!!!
Code: Select all
    var temp = document.callmeback_form.telephone.value;
        
    if (!temp.match(/^(\+44[0-9]{10}|0044[0-9]{10}|0[0-9]{10})$/)) 
    {
    alert("Enter a valid UK Phone number including area code");
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width = "132px";
    document.callmeback_form.telephone.style.height = "15px";
    return false;
    }
But now that I try to make the parts that alert the user when the enter to many or to few numbers it goes all wrong.

I just want them to get an alert if they put more than 13 characters in for +44 , 14 characters for 0044 , and 11 characters for hte normal 0 number.

im actually starting to get this javascript malarky. although I think you boys are safely well ahead of me!!

Thanks AGAIN. Dan.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Oct 26th, 2007, 11:18
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

here comes a regex explanation:
"/" --> start deliminator

"^" --> anchor at the beginning of the string
"(" --> open IF statement
"\+44" --> literal character string "+44"
"[0-9]{9}" --> match 9 digits
"|" --> OR
"0044" --> literal character string "0044"
"[0-9]{10}" --> match 10 digits
")" --> close IF statement
"$" --> anchor end of string
"/" --> end deliminator
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Oct 26th, 2007, 11:25
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

ok when when you say "[0-9]{10}" --> match 10 digits your telling javascript to expect 10 digits after the literal character string??

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Oct 26th, 2007, 11:27
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

LOL - tag team..

Yep, that's exactly what this is doing. It's not a character string per se, it's a character class.

It is the characters 0-9
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Oct 26th, 2007, 11:30
Up'n'Coming Member
Join Date: Oct 2007
Location: london
Age: 25
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

yay! ive got something right! lol

Any ideas on how to get the code to check if the numbers are to long or short from this point now??

thanks. Dan.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Oct 26th, 2007, 11:32
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: playing with Perl Compatible Regex in field validation

The thing is that the regex will check the pattern and if the patter matches the string will be the correct length.

If you wanted you could throw in one more (possibly redundant) check.

Code: Select all
   

 var temp = document.callmeback_form.telephone.value;
        
    if (!temp.match(/^(\+44[0-9]{10}|0044[0-9]{10}|0[0-9]{10})$/) || temp.length < 12 || temp.length > 14) 
    {
    alert("Enter a valid UK Phone number including area code");
    document.callmeback_form.telephone.focus();
    document.callmeback_form.telephone.style.background = '#FF8600';
    document.callmeback_form.telephone.style.border = "1px solid #A5ACB2";
    document.callmeback_form.telephone.style.width = "132px";
    document.callmeback_form.telephone.style.height = "15px";
    return false;
    }
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Oct 26th, 2007 at 11:35.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!