Regex validation code

e107 CMS » Forums » e107 v1.x Support » Core Code Support << Previous thread | Next thread >>
TropetaOntre
May 13 2012, 01:05PM
Registered Member #145832
Joined: May 13 2012, 12:57PM
Posts: 4
Hi all

I am having difficulties creating a regex validation code for a extended user-field.

I need my members to enter a text that matches the following pattern:
upper/lower case letters or numbers.4digit number.

example: User.1234 - I need a regex code that validates the .1234 entry. I know that the dot is special in regex and you need to escape it with a backslash, but so far I was unable to create an entry that forces a user to enter a text and end with a dot follow by exactly 4 digit number.

The closest I got is:
#^[a-zA-Z0-9]+\.[0-9]{4}$#

I tried various sites, so Google doesn't help in this case, I need someone to literally tell me the expression that I need to enter.

Thank you in advance for taking a noob user request.

[ Edited May 13 2012, 01:07PM ]
Go to top
zollk60
May 13 2012, 02:33PM
Registered Member #27052
Joined: Apr 27 2006, 05:11PM
Location: Northern NY
Posts: 121
Try this
^[a-zA-Z0-9]+\.\d{4}$
[ Edited May 13 2012, 02:38PM ]
Go to top
TropetaOntre
May 13 2012, 02:53PM
Registered Member #145832
Joined: May 13 2012, 12:57PM
Posts: 4
zollk60 wrote ...

Try this
^[a-zA-Z0-9]+\.\d{4}$


Thank you for the reply, unfortunately it doesn't work. I also tried adding # as I saw in the wiki example:
#^[a-zA-Z0-9]+\.\d{4}$#

I am pretty stuck, no clue what I am doing wrong, basically I want to validate any entry that uses a dot followed by a 4 digit number.
Go to top
mfp
May 13 2012, 03:15PM
  • e107 Translation Team
Registered Member #24174
Joined: Jan 27 2006, 01:23AM
Location: novara,italy
Posts: 84
Try this:

/^[a-zA-Z0-9]+\.\d{4}$/


[ Edited May 13 2012, 03:17PM ]
Go to top
Website
TropetaOntre
May 13 2012, 03:26PM
Registered Member #145832
Joined: May 13 2012, 12:57PM
Posts: 4
mfp wrote ...

Try this:
/^[a-zA-Z0-9]+\.\d{4}$/



That works, thank you very much both!
(it's annoying when you can't see the little things)
[ Edited May 13 2012, 03:27PM ]
Go to top
mfp
May 13 2012, 03:32PM
  • e107 Translation Team
Registered Member #24174
Joined: Jan 27 2006, 01:23AM
Location: novara,italy
Posts: 84
Fine! Glad to be helpful...
Go to top
Website
MysterF
May 13 2012, 06:28PM
Registered Member #54659
Joined: Jan 31 2010, 02:19PM
Posts: 320
Only 1 observation, you said that you want to match the dot followed by 4 digits, problem is that you are also restricting the entry to a single word. For example you can't enter New User.1234 only NewUser.1234. I am facing same problem and judging by your example you want to match Guild Wars 2 Account display name (i.e.: MysterF.1234). I also added .-_ as allowed character for the 1st part of the regex, but I am stuck from that point.
/^[a-zA-Z0-9._-]+\.\d{4}$/

No clue what needs to be added to allow space though (and work with e107).

PS
You can avoid the field name display if you want to expand this to your other site areas and not just the user profile:

If you want it to be displayed like that, add in your forum_viewtopic_template.php next to your {POSTER} ({USER_EXTENDED=gw2acc.value}) - as you can see I didn't used .text_value, only .value, which now removed the text attached to this user-field; gw2acc is the name of the user-field, you can replace it with the one you already created.

Hopefully one of the previous posters will jump with a solution for the 2 words (or space) possibility, but not condition, so a user should be able to type a 1 word name followed by a dot plus 4 digit number or 2 words name followed by a dot plus a 4 digit number.




[ Edited May 13 2012, 06:29PM ]
Go to top
TropetaOntre
May 13 2012, 06:38PM
Registered Member #145832
Joined: May 13 2012, 12:57PM
Posts: 4
bummer... and I thought I was done.

YES I am doing this for guild wars 2, no clue how you managed to deduct this, but thank you for the suggestion it looks awesome.

If someone has an idea on how we could allow but not condition 2 words in that regex or at least allow the space
Go to top
MysterF
May 13 2012, 06:47PM
Registered Member #54659
Joined: Jan 31 2010, 02:19PM
Posts: 320
/^[a-zA-Z0-9 ._-]+\.\d{4}$/


That should now work, I simply added a space after 9 and it works, it now allows either 1 or 2 word names.
Thanks for giving me a push I had this on hold for a while now. {b}
Go to top
 

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System