473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Regular Expressions Issue...

chunk1978
224 100+
hi everyone... i'm preparing to complete a validated form through client-side javascript with regular expressions... and yes the form will also be validated server-side as well... anyway, my regex code is problematic, and i was wondering if someone who know about regular expressions really well could have a look at it and tell me what is the problem.

Expand|Select|Wrap|Line Numbers
  1.  
  2. function checkform ( form )
  3.     {
  4.     if (!/[a-zA-Z]+$/.test(form.name.value)) {
  5.     alert( "Please enter a valid NAME." );
  6.     form.name.focus();
  7.     return false ;}
  8.  
  9.     if (!/[\w\s]+$/.test(form.addressline1.value)) {
  10.     alert( "Please enter a valid ADDRESS." );
  11.     form.addressline1.focus();
  12.     return false ;}
  13.  
  14.     if (!/[a-zA-Z]+$/.test(form.city.value)) {
  15.     alert( "Please enter a valid CITY." );
  16.     form.city.focus();
  17.     return false ;}
  18.  
  19.     if (!/[a-zA-Z]+$/.test(form.provincestate.value)) {
  20.     alert( "Please enter a valid PROVINCE/STATE." );
  21.     form.provincestate.focus();
  22.     return false ;}
  23.  
  24.     if (!/[\w\s]+$/.test(form.postalzip.value)) {
  25.     alert( "Please enter a valid POSTAL/ZIP CODE." );
  26.     form.postalzip.focus();
  27.     return false ;}
  28.  
  29. (etc...)
  30.  
  31.  
for the NAME, CITY, and PROVINCE/STATE lines, i am trying to only allow for alphabetic strings (more than one string)... however, in testing i found that i can enter "New York City", where as i can not enter "123 #$% 789"... but unfortunately this code will also allow me to enter "123New $%^York 789City", so it's not functioning correctly...

i'm fairly new to regular expressions, and i would love to keep the code in it's current format to only validate on form submit, but either regular expressions is a buggy option, or i'm clearly missing something... (i'd vote on the latter)...

please help if you can... thanks
Jan 18 '07 #1
20 2324
AricC
1,892 Expert 1GB
Without looking at the regex, why are you validating this data 2x? The reason I use Javascript to validate my web forms is to keep the load on the client machine not the server.
Jan 18 '07 #2
acoder
16,027 Expert Mod 8TB
You need two anchors, you are only using one.
The two anchors are ^ and $, ^ is the start anchor and $ is the end anchor. At the moment, your regex matches anything ending with an alphabetic character.
So, use
Expand|Select|Wrap|Line Numbers
  1. if (!/^[a-zA-Z]+$/.test(form.name.value)) {
Jan 18 '07 #3
acoder
16,027 Expert Mod 8TB
Without looking at the regex, why are you validating this data 2x? The reason I use Javascript to validate my web forms is to keep the load on the client machine not the server.
You need to validate data twice. If anything, validate only on the server side, users could have Javascript disabled.

See this link for more info.
Jan 18 '07 #4
chunk1978
224 100+
You need two anchors, you are only using one.
The two anchors are ^ and $, ^ is the start anchor and $ is the end anchor. At the moment, your regex matches anything ending with an alphabetic character.
So, use
Expand|Select|Wrap|Line Numbers
  1. if (!/^[a-zA-Z]+$/.test(form.name.value)) {
hey... your code works, but it only allows me to enter one string of characters... so under name i can enter "John", but i can't enter "John Doe Smith"... the same goes for city... for city i can enter "Seattle" but i can't enter "San Francisco"... so how do i rewrite the regular expressions to allow for more than one string of characters?

thanks
Jan 18 '07 #5
acoder
16,027 Expert Mod 8TB
hey... your code works, but it only allows me to enter one string of characters... so under name i can enter "John", but i can't enter "John Doe Smith"... the same goes for city... for city i can enter "Seattle" but i can't enter "San Francisco"... so how do i rewrite the regular expressions to allow for more than one string of characters?

thanks
For that you will need to allow spaces. This is one example, but others may come up with something more compact:
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{1}[a-zA-Z])*$
What this does is match an alphabetic character and then possibly matches 1 space followed by more alphabetic characters. The asterisk * means 0 or more. + means one or more. {1} means match exactly once.
Jan 18 '07 #6
chunk1978
224 100+
For that you will need to allow spaces. This is one example, but others may come up with something more compact:
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{1}[a-zA-Z])*$
What this does is match an alphabetic character and then possibly matches 1 space followed by more alphabetic characters. The asterisk * means 0 or more. + means one or more. {1} means match exactly once.
ok... so if i understand correctly...


Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{1}[a-zA-Z])*$
means that there must be 1 space between two alphabetic characters. so someone could enter "hi there" or "how's you?" into the textarea (they can not enter "hi" or "hi how's it going")

Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{+}[a-zA-Z])*$
means that there must be at least one space between two or more alphabetic characters = "hi there" or "hi how's it going?" (but not "hi").

Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{*}[a-zA-Z])*$
means that there can be some spaces or not, so someone could enter "hi", as well as "hi how's it going?" into the textarea.



i'm just asking now because i have to go out so i can't test it, and i'll be staying up all night when there's less live help on this site...
Jan 18 '07 #7
chunk1978
224 100+
sorry, read your last instructions too quick... i though they all were in parenthesis, but only the number signifying spaces are (IE "{1}")... i got it now... thanks


Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{1}[a-zA-Z])*$
means that there must be 1 space between two alphabetic characters. so someone could enter "hi there" or "how's you?" into the textarea (they can not enter "hi" or "hi how's it going")
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]+[a-zA-Z])*$
means that there must be at least one space between two or more alphabetic characters = "hi there" or "hi how's it going?" (but not "hi").
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]*[a-zA-Z])*$
means that there can be some spaces or not, so someone could enter "hi", as well as "hi how's it going?" into the textarea.
Jan 19 '07 #8
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]{1}[a-zA-Z])*$
means that there must be 1 space between two alphabetic characters. so someone could enter "hi there" or "how's you?" into the textarea (they can not enter "hi" or "hi how's it going")
Actually they can enter "hi" because the whole of the second part is in parentheses with an asterisk, so the second part is an optional match. Actually, "how's it going?" would not match because of the apostrophe and question mark.
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]+[a-zA-Z])*$
means that there must be at least one space between two or more alphabetic characters = "hi there" or "hi how's it going?" (but not "hi").
Yes that's right, but remember again about the ' and ? (I think you're just giving examples, but I thought I'd point it out anyway).
Expand|Select|Wrap|Line Numbers
  1. ^[a-zA-Z]+([ ]*[a-zA-Z])*$
means that there can be some spaces or not, so someone could enter "hi", as well as "hi how's it going?" into the textarea.
Yes, again that's right. You're getting the hang of it.

Have you tested it and does it work?
Jan 19 '07 #9
chunk1978
224 100+
Have you tested it and does it work?
yeah it's getting a lot easier... and it works... but i'm concerned that my code isn't neat, and therefore making the function work too hard... for example:

Expand|Select|Wrap|Line Numbers
  1. if (!/^[a-zA-Z0-9éèàïêâôÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\#)]+([ ]*[a-zA-Z0-9éèàïêâôÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\#)])*$/.test(form.addressline1.value)) {
  2.     alert( "Please enter a valid ADDRESS." );
  3.     form.addressline1.focus();
  4.     return false ;}
  5.  
is my current code for an address line. my website is going to be bilingual (English/French) so i'm allowing french characters... i'm also allowing hyphens, because a lot of streets here in Montréal are hyphenated (IE rue Ste-Catherine)... i'm also allowing number signs, commas, apostrophes, and periods... (IE rue Lamber's Closs blvd., Apartment #3)... it's making me a bit tense though, because i'm unaware of which characters are needed to allow for malicious code if someone wanted to trash my files/server...

also, i started a new discussion because there are optional fields on my form, and i'm not sure how to use regular expressions with an optional field... it seems to me that if regular expressions are implanted for the field, the field must be filled... am i wrong? i would really like to use regular expressions for the optional fields as well...
Jan 19 '07 #10
acoder
16,027 Expert Mod 8TB
Why not split your address field to two or more fields, e.g. house name/no., street name, etc. It might make it easier, but considering you have so many possibilities, it may not make too much of a difference.

You're worried about SQL Injection. Read more about it here.
Jan 19 '07 #11
chunk1978
224 100+
i'm just assuming that i'd be safe by not allowing forward or backward slashes, semi-colins, parenthesis, dollar signs, asterisks, carets, percent signs, etc... i hope my assumptions are correct.
Jan 19 '07 #12
acoder
16,027 Expert Mod 8TB
Don't forget the single apostrophe ('). It should be allowed, e.g. Prince's Avenue, but you need to deal with it on the server side to make sure that any malicious input cannot misuse it.
Jan 19 '07 #13
chunk1978
224 100+
Don't forget the single apostrophe ('). It should be allowed, e.g. Prince's Avenue, but you need to deal with it on the server side to make sure that any malicious input cannot misuse it.

i seem to be still having issues with the additional comments field... the field allows up to 500 characters to be entered, but the regex doesn't seem to be working as i want it to...

Expand|Select|Wrap|Line Numbers
  1.  
  2.     if (!/^[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\")(\?)(\!)]+([ ]*[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\")(\?)(\!)])*$/.test(form.additionalcomments.value)) {
  3.     alert( "Please remove Illegal characters from ADDITIONAL COMMENTS field.  Allowed characters include Letters, Numbers, Hyphens, Quotations, Apostrophes, Periods, Commas, Question and Esclimation Marks." );
  4.     form.additionalcomments.focus();
  5.     return false;}
  6.  
  7.  
any suggestions?
Jan 19 '07 #14
chunk1978
224 100+
nevermind... i realized i had to allow for whitespaces and thus change the code to the following...

Expand|Select|Wrap|Line Numbers
  1.  
  2.     if (!/^[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\s(\-)(\.)(\')(\,)(\")(\?)(\!)]+([ ]*[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\s(\-)(\.)(\')(\,)(\")(\?)(\!)])*$/.test(form.additionalcomments.value)) {
  3.     alert( "Please remove Illegal characters from ADDITIONAL COMMENTS field.  Allowed characters include Letters, Numbers, Hyphens, Quotations, Apostrophes, Periods, Commas, Question and Esclimation Marks." );
  4.     form.additionalcomments.focus();
  5.     return false;}
  6.  
  7.  
Jan 19 '07 #15
acoder
16,027 Expert Mod 8TB
nevermind... i realized i had to allow for whitespaces and thus change the code to the following...
Expand|Select|Wrap|Line Numbers
  1. if (!/^[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\s(\-)(\.)(\')(\,)(\")(\?)(\!)]+([ ]*[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\s(\-)(\.)(\')(\,)(\")(\?)(\!)])*$/.test(form.additionalcomments.value)) 
  2.  
Are you sure you want to use \s?

\s is short for \f\n\r\t\v and some others which includes form feed, line feed, carriage return, horizontal tab and vertical tab.

Also, you can make it smaller (though not by much) by using i at the end, i.e. /i to denote case-insensitive search.
Jan 19 '07 #16
chunk1978
224 100+
Are you sure you want to use \s?.
i'm not exactly sure... the textarea i'm using this regex on is a big text box, not just a single line... i want to allow the user to use the return key to start a new line, and maybe tabs if they so choose... without \s the field doesn't allow that... although i'm totally open to better suggestions.
Jan 19 '07 #17
acoder
16,027 Expert Mod 8TB
i'm not exactly sure... the textarea i'm using this regex on is a big text box, not just a single line... i want to allow the user to use the return key to start a new line, and maybe tabs if they so choose... without \s the field doesn't allow that... although i'm totally open to better suggestions.
In that case, that should be fine. I only asked because you had mentioned earlier about spaces only.
Jan 19 '07 #18
chunk1978
224 100+
In that case, that should be fine. I only asked because you had mentioned earlier about spaces only.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     if (!/^[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\")(\?)(\!)]+([ ]*[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\")(\?)(\!)])*$/.test(form.additionalcomments.value)) {
  3.     alert( "Please remove Illegal characters from ADDITIONAL COMMENTS field.  Allowed characters include Letters, Numbers, Hyphens, Quotations, Apostrophes, Periods, Commas, Question and Esclimation Marks." );
  4.     form.additionalcomments.focus();
  5.     return false;}
  6.  
  7.  
now im having the strangest issue with this one regex for the additional comments text area... i've reduced the characters allowed to 300, instead of 500... but if i enter a few sentences, and if at the end of the comments box there is an illegal character (IE "#"), the normal alert box that is suppose to suddenly show up takes a few seconds to do so, and sometimes i noticed that it wouldn't be able to process and the browser would say the script isn't able to continue and crashes the script... i'm using Safari 2.0, and Firefox 2.0, on an iMac Intel Core 2 Duo (2.0GHZ)...

however, it also seems that if there's an illegal character at or near the beginning of the inputed text, the alert is instant and normal... is the problem that the regex code is too long? not neat enough? have you any suggestions to make it cleaner or compact?
Jan 20 '07 #19
acoder
16,027 Expert Mod 8TB
You can use a case-insensitive match using the i flag
Expand|Select|Wrap|Line Numbers
  1. /^[a-z0-9çéèàïêâô\-\.\'\,\"\?\!]+([ ]*[a-z0-9çéèàïêâô\-\.\'\,\"\?\!])*$/i
Jan 20 '07 #20
chunk1978
224 100+
You can use a case-insensitive match using the i flag
Expand|Select|Wrap|Line Numbers
  1. /^[a-z0-9çéèàïêâô\-\.\'\,\"\?\!]+([ ]*[a-z0-9çéèàïêâô\-\.\'\,\"\?\!])*$/i
great! didn't know about the \i... it defiantly shrinks all my regexes down a lot... thanks for the tip...

unfortunately it didn't fix the hangup/freeze for this additional comments box... the script hangs for a second or two if an illegal character is placed near the end of the input text which allows 300 characters and also "\s" (returns, etc.)... i don't think it's a big deal though, right now i'm more curious as to why it's happening than trying to fix it...

this is the regex for this js function:

Expand|Select|Wrap|Line Numbers
  1.     if (document.IMAGECtrlForm.additionalcomments.value != "") {
  2.     if (!/^[a-z0-9çéèàïêâô\-\.\'\,\"\?\!\s]+([ ]*[a-z0-9çéèàïêâô\-\.\'\,\"\?\!\s])*$/i.test(form.additionalcomments.value)) {
  3.     alert( "(bla bla bla)" );
  4.     form.additionalcomments.focus();
  5.     return false;}}
  6.  
any ideas?
Jan 21 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dmbkiwi | last post by:
I have a problem. I have written a python based theme for a linux app called superkaramba, which is effectively an engine for desktop applets that utilises python as its theming language. The...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
2
by: norton | last post by:
Hi, I am learning Regular Expression and currently i am trying to capture information from web page. I wrote the following code to capture the ID as well as the Title Dim regex = New Regex( _...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
2
by: Prakash | last post by:
Dear Friends, In my web application i have two textboxes to collect "ORIGIN" and "DESTINATION" stations. These stations are represented as three letter code.. We have an option like to provide...
12
by: =?Utf-8?B?SlA=?= | last post by:
I am a newbie to regular expressions and want to extract a number from the end of a string. The string would have these formats: image/4567 image/45678 image/456789 I would also want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.