The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Regex combinations
Terminator
post Apr 6 2016, 12:04 AM
Post #1


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



For a different project I want to strip not only all spaces from a string, but numbers and symbols too. I basically just want to allow letters only, uppercase and lowercase.

I know I can use [a-z] with regex, but I cant figure out how to combine it with my current code below.

Don't I have to do something like [a-zA-z] and then probably need a "not" ! in there? But also I want to make sure to strip all whitespace too.

CODE

var str = ($("str").value.replace(/\s/g, ''));


thanks
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 6 2016, 06:35 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Terminator @ Apr 6 2016, 07:04 AM) *

Don't I have to do something like [a-zA-z] and then probably need a "not" ! in there?

Yes, I think you might use

CODE
replace(/[^a-zA-Z]/g, '')

The ^ character is used for negation when inside square brackets. Without square brackets, a ^ character indicates the beginning.

Of course, things get problematic if users insert other letters than a-zA-Z. Even in English speaking countries people may use foreign words or names, or use symbols like ' for abbreviations of English words.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Terminator
post Apr 6 2016, 03:31 PM
Post #3


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



Thanks that worked for what I needed it, but I have another question.

If I were doing this on a form where people input names, and since some last names have punctuation like O'Brien or a combined last name like Cameron-Bure, would I put is like this? (to also allow ' and -)

CODE

var lastName = ($("lastName").value.replace(/[^a-zA-Z'-]/g, ''));


But there would be one issue because some first and last name can have a space in them. Like Mary Anne for example.

Would I use the JavaScript trim method so I can trim out any white space from beginning and end, but leave the space for a name like Mary Anne?

But can you combine trim and replace?

Or would I have to do

CODE

var lastName = ($("lastName").value.replace(/[^a-zA-Z'-\s]/g, ''));


which would allow spaces, and then trim method on it right after?

thanks

This post has been edited by Terminator: Apr 6 2016, 03:32 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 6 2016, 04:31 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Terminator @ Apr 6 2016, 10:31 PM) *

would I put is like this? (to also allow ' and -)

CODE

var lastName = ($("lastName").value.replace(/[^a-zA-Z'-]/g, ''));


I'm just a novice in regex, but I think that would work. You can add any characters as long as they don't have a special regex meaning (in which case they need to be escaped). The hyphen character does have special meaning, but only for ranges (e.g. 0-9) and when inside square brackets.

QUOTE
But there would be one issue because some first and last name can have a space in them. Like Mary Anne for example.

Would I use the JavaScript trim method so I can trim out any white space from beginning and end, but leave the space for a name like Mary Anne?

Yes, but then you need to whitelist space characters as well. Perhaps [^a-zA-Z' -] could be used? (Note that I put the space between the ' and -, if I put it last I got a script error in my browser for some reason.)

QUOTE
But can you combine trim and replace?

Not sure I understood, but you might use a regex in replace() that only removes beginning and ending spaces, and then you won't need trim().

QUOTE
Or would I have to do

CODE

var lastName = ($("lastName").value.replace(/[^a-zA-Z'-\s]/g, ''));


which would allow spaces,

Yes, except that s\ allows all space characters, including newlines etc.

QUOTE
and then trim method on it right after?

Yes I think you should use trim() last, since removing non-letter characters may cause previously internal spaces to appear at the beginning or end. Perhaps something like this could work:

CODE
var lastName = ($("lastName").value.replace(/[^a-zA-Z' -]/g, '').trim());

? This will of course allow users to enter things like M a r y A n n e...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Terminator
post Apr 6 2016, 05:03 PM
Post #5


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



QUOTE(Christian J @ Apr 6 2016, 04:31 PM) *


CODE
var lastName = ($("lastName").value.replace(/[^a-zA-Z' -]/g, '').trim());

? This will of course allow users to enter things like M a r y A n n e...


That code worked, it allowed the extra punctuation and it trims white space from beginning and end of name, but like you said it allows them to type extras spaces like M a r y A n n e.

I will probably just have to go with this, which allows the extra punctuation but removes all spaces:

CODE

var lastName = ($("lastName").value.replace(/[^a-zA-Z'-]/g, ''));


So when I make the firstName var for this it will just have to save Mary Anne as MaryAnne I guess. I am eventually going to have this data saved to a database from the website.

I am going to give them the option to review and change their input, so they can always put "Anne" as the middle name, which would then separate it.

But thanks, you helped lead me along the right way on this.

This post has been edited by Terminator: Apr 6 2016, 05:05 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 6 2016, 06:08 PM
Post #6


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



QUOTE(Terminator @ Apr 7 2016, 12:03 AM) *

So when I make the firstName var for this it will just have to save Mary Anne as MaryAnne I guess.

One idea might be to preserve spaces before capital letters (except the first), but I think in the end one must trust users to spell their names properly. After all they could make syntactically valid typos as well (like Marry Ane) that are impossible to detect.

QUOTE
But thanks, you helped lead me along the right way on this.

You're welcome. smile.gif

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Terminator
post Apr 7 2016, 12:05 AM
Post #7


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



QUOTE(Christian J @ Apr 6 2016, 06:08 PM) *

One idea might be to preserve spaces before capital letters (except the first), but I think in the end one must trust users to spell their names properly. After all they could make syntactically valid typos as well (like Marry Ane) that are impossible to detect.


That is a good idea too, and it brings something else up. If I want the name stored in the database as John Smith, and they input it as jOhN sMITh on the website, I am going to have to do more conversions.

I guess my best option would be to convert the entire string to lowercase, and then make the first letter uppercase. First and last names will be separate strings, but the name MaryAnne will then always be saved as Maryanne. And then if they have a last name with a dash like Cameron-Bure, it will be stored as Cameron-bure. But I can probably add something that tells it to capitalize the first letter after the dash, and even apostrophe for names like O'Brien.

For the address, I think I will just make it all uppercase. I noticed that is what Amazon and others do.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Apr 7 2016, 06:16 AM
Post #8


.
********

Group: WDG Moderators
Posts: 9,661
Joined: 10-August 06
Member No.: 7



Thinking of this from another angle, what's the purpose of these users entering (their own?) names? A few scenarios:

* If the name is going to be compared with an existing database entry, maybe that comparison is all you need to do.

* If you can't verify that it's a valid name but the spelling is still important (why?), I guess you have no choice but to trust the user.

* If it's just a nick name for e.g. a forum account maybe it doesn't really matter what the user calls himself (withing technical limits).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Terminator
post Apr 7 2016, 11:51 AM
Post #9


Advanced Member
****

Group: Members
Posts: 218
Joined: 19-March 15
Member No.: 22,398



QUOTE(Christian J @ Apr 7 2016, 06:16 AM) *

Thinking of this from another angle, what's the purpose of these users entering (their own?) names? A few scenarios:

* If the name is going to be compared with an existing database entry, maybe that comparison is all you need to do.

* If you can't verify that it's a valid name but the spelling is still important (why?), I guess you have no choice but to trust the user.

* If it's just a nick name for e.g. a forum account maybe it doesn't really matter what the user calls himself (withing technical limits).


I am going to make a practice E-Commerce Site which has products and prices stored in a database, and I am going to store their customer info in the database for the new customers after they enter name and address, and pick items in a shopping cart. Will probably let them choose a login name too, but I am going to need to validate the info that the new customers type in.

I am going to do another one for a dentist office, so I will probably validate their info on an appointment form they fill out on the site.

I am also going to do php server side validation for these.

This post has been edited by Terminator: Apr 7 2016, 11:51 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 08:20 PM