Regular Expressions Issue...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chunk1978
    New Member
    • Jan 2007
    • 224

    Regular Expressions Issue...

    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.

    Code:
    function checkform ( form )
    	{
    	if (!/[a-zA-Z]+$/.test(form.name.value)) {
        alert( "Please enter a valid NAME." );
        form.name.focus();
        return false ;}
    	
    	if (!/[\w\s]+$/.test(form.addressline1.value)) {
        alert( "Please enter a valid ADDRESS." );
        form.addressline1.focus();
        return false ;}
      
    	if (!/[a-zA-Z]+$/.test(form.city.value)) {
        alert( "Please enter a valid CITY." );
        form.city.focus();
        return false ;}
    	
    	if (!/[a-zA-Z]+$/.test(form.provincestate.value)) {
        alert( "Please enter a valid PROVINCE/STATE." );
        form.provincestate.focus();
        return false ;}
    	
    	if (!/[\w\s]+$/.test(form.postalzip.value)) {
        alert( "Please enter a valid POSTAL/ZIP CODE." );
        form.postalzip.focus();
        return false ;}
    
    (etc...)
    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
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    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.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      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
      Code:
      if (!/^[a-zA-Z]+$/.test(form.name.value)) {

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by AricC
        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.

        Comment

        • chunk1978
          New Member
          • Jan 2007
          • 224

          #5
          Originally posted by acoder
          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
          Code:
          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

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by chunk1978
            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:
            Code:
            ^[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.

            Comment

            • chunk1978
              New Member
              • Jan 2007
              • 224

              #7
              Originally posted by acoder
              For that you will need to allow spaces. This is one example, but others may come up with something more compact:
              Code:
              ^[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...


              Code:
              ^[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")

              Code:
              ^[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").

              Code:
              ^[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...

              Comment

              • chunk1978
                New Member
                • Jan 2007
                • 224

                #8
                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


                Code:
                ^[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")
                Code:
                ^[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").
                Code:
                ^[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.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by chunk1978
                  Code:
                  ^[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.
                  Originally posted by chunk1978
                  Code:
                  ^[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).
                  Originally posted by chunk1978
                  Code:
                  ^[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?

                  Comment

                  • chunk1978
                    New Member
                    • Jan 2007
                    • 224

                    #10
                    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:

                    Code:
                    if (!/^[a-zA-Z0-9éèàïêâôÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\#)]+([ ]*[a-zA-Z0-9éèàïêâôÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\#)])*$/.test(form.addressline1.value)) {
                        alert( "Please enter a valid ADDRESS." );
                        form.addressline1.focus();
                        return false ;}
                    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...

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      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.

                      Comment

                      • chunk1978
                        New Member
                        • Jan 2007
                        • 224

                        #12
                        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.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          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.

                          Comment

                          • chunk1978
                            New Member
                            • Jan 2007
                            • 224

                            #14
                            Originally posted by acoder
                            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...

                            Code:
                            	if (!/^[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\")(\?)(\!)]+([ ]*[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\-(\.)(\')(\,)(\")(\?)(\!)])*$/.test(form.additionalcomments.value)) {
                            	alert( "Please remove Illegal characters from ADDITIONAL COMMENTS field.  Allowed characters include Letters, Numbers, Hyphens, Quotations, Apostrophes, Periods, Commas, Question and Esclimation Marks." );
                            	form.additionalcomments.focus();
                            	return false;}
                            any suggestions?

                            Comment

                            • chunk1978
                              New Member
                              • Jan 2007
                              • 224

                              #15
                              nevermind... i realized i had to allow for whitespaces and thus change the code to the following...

                              Code:
                              	if (!/^[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\s(\-)(\.)(\')(\,)(\")(\?)(\!)]+([ ]*[a-zA-Z0-9çéèàïêâôÇÉÈÀÏÊÂÔ\s(\-)(\.)(\')(\,)(\")(\?)(\!)])*$/.test(form.additionalcomments.value)) {
                              	alert( "Please remove Illegal characters from ADDITIONAL COMMENTS field.  Allowed characters include Letters, Numbers, Hyphens, Quotations, Apostrophes, Periods, Commas, Question and Esclimation Marks." );
                              	form.additionalcomments.focus();
                              	return false;}

                              Comment

                              Working...