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



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

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
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.
Reply With Quote

  #2 (permalink)  
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.
Reply With Quote
  #3 (permalink)  
Old Oct 25th, 2007, 19:00
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
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>
Reply With Quote
  #4 (permalink)  
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???
Reply With Quote
  #5 (permalink)  
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.
Reply With Quote
  #6 (permalink)  
Old Oct 26th, 2007, 10:39
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: playing with Perl Compatible Regex in field validation

Amen, bro. Regex RockZ!!!
Reply With Quote
  #7 (permalink)  
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!!
Reply With Quote
  #8 (permalink)  
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)
Reply With Quote
  #9 (permalink)  
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.
Reply With Quote
  #10 (permalink)  
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)
Reply With Quote
  #11 (permalink)  
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.
Reply With Quote
  #12 (permalink)  
Old Oct 26th, 2007, 11:18
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
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
Reply With Quote
  #13 (permalink)  
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??

Reply With Quote
  #14 (permalink)  
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)
Reply With Quote
  #15 (permalink)  
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.
Reply With Quote
  #16 (permalink)  
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.
Reply With Quote
  #17 (permalink)  
Old Oct 26th, 2007, 11:37
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

Ah ive seen what you have done. But the three different number styles have different numbers of characters overall. eg. a +44 number has 13 characters whilst the 0044 number has 14 , and the normal entry has just 11. T
his is why its such a pain. Especially when I have to warn the user when the value is below or above the correct number!!

Thanks.Dan
Reply With Quote
  #18 (permalink)  
Old Oct 26th, 2007, 11:49
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 now how to implement the less or more than function when using the regex, but have different values for each number type???

Also ive added another number type in and i have broken the code
Code: Select all
    if (!temp.match(/^(\+44[0-9]{10}|0044[0-9]{10}|0[0-9]{10}|+353[0-9]{8})$/))
Have I messed up the syntax???
Reply With Quote
  #19 (permalink)  
Old Oct 26th, 2007, 11:49
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

Do you have to though? lol...

If it's really required, can do your length checks after the match fails..

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})$/) ) 
    {
        // Okay so the first match failes.. let's see why
               alert("Enter a valid UK Phone number including area code");


          if (temp.match(/^+44/) && temp.length != 13)
             alert ("Numbers with this prefix (+44) need 13 characters");
          if (temp.match(/^0044/) && temp.length != 14)
             alert ("Numbers with the prefix (0044) need 14 characters.");
          else if (temp.length != 11)
              alert ("Numbers including area code should be 11 characters");


    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)
Reply With Quote
  #20 (permalink)  
Old Oct 26th, 2007, 12:23
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

Hey Rakuli, it's me AGAIN.

can you have a look at this for me... For some reason that I cant spot the problem. The only alerts that seems to work are the first two.. why is this?? Have I done something stupid again???

Code: Select all
var temp = document.callmeback_form.telephone.value;
      
     if (temp=="")
     {
     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;
    }
      
    if (!temp.match(/^(\+44[0-9]{10}|0044[0-9]{10}|0[0-9]{10})$/) ) 
    {


          if (temp.match(/^(\+44)$/) && temp.length < 13)
             {
             alert("The number you have entered with this prefix (+44) is too short, please check and try again");
                return false;
             }
             
          if (temp.match(/^(\+44)$/) && temp.length > 13)
             {
             alert("The number you have entered with this prefix (+44) is too long, please check and try again");
             return false;
             }
             
          if (temp.match(/^(\0044)$/) && temp.length < 14)
             {
             alert("The number you have entered with this prefix (0044) is too short, please check and try again");
             return false;
             }
          if (temp.match(/^(\0044)$/) && temp.length > 14)
               {
             alert("The number you have entered with this prefix (0044) is too long, please check and try again");
             return false;
             }
             
    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;
    }
Thanks. Dan
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Regex alexgeek JavaScript Forum 3 Dec 23rd, 2007 16:52
[SOLVED] Field validation and changing display properties... c_martini JavaScript Forum 12 Sep 25th, 2007 11:27
Non-text field Validation NewDesigner JavaScript Forum 6 Nov 24th, 2006 22:34
[SOLVED] Convert script to NS6 compatible - Please Help! Anonymous User JavaScript Forum 1 Feb 20th, 2005 02:35
Field Validation HELP! Monie JavaScript Forum 5 Nov 11th, 2004 19:46


All times are GMT. The time now is 22:44.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved