PDA

View Full Version : [CLOSED] Regex possibly not working?



EddieJensen
Aug 28, 2014, 3:31 AM
Hi,

I am working with a C# app using MVC and Ext.Net in Razor views.
I have a text field:


.Add(Html.X().TextField()
.FieldLabel("Mobile")
.Text(Model.mobilePhone)
.Width(400)
.Y(30)
.Regex(@"\+?([0-9](\s|-)*)*(ext|x)?(\s)*[0-9]+")
.RegexText("Valid Format: +1 800 5551212 ext3333")
.Name("mobilePhone")
.Listeners(lst => lst.Added.Handler = @"container.myMobilePhone = this;")
)


The user should be able to type a phone number in this field and I have provided a regex to validate the number.
When I test this regex in a javascript regex tester, it matches the type of phone numbers I want it to.
but When the project loads, the regex is not being activated correctly.
If you type numbers, then letters, the field is not validated e.g. 2342 234 2sdg 234 should fail validation
If you START with a letter e.g. "sdg2342 234 23" then it the field is highlighted and the RegexText message is displayed, otherwise it just doesn't work.

Does anyone have any idea's what I am doing wrong here?
Thank you in advance!
Cheers,
Eddie

Daniil
Aug 28, 2014, 8:19 AM
Hi @EddieJensen,


When I test this regex in a javascript regex tester, it matches the type of phone numbers I want it to.


e.g. 2342 234 2sdg 234 should fail validation

I see "true" in a alert box:

var reg = /\+?([0-9](\s|-)*)*(ext|x)?(\s)*[0-9]+/;

alert(reg.test("2342 234 2sdg 234"));

So, the string passes the regular expression, so, it passes validation.

EddieJensen
Aug 28, 2014, 7:54 PM
Hi Daniil,

Thanks for your reply. I think what you are trying to say is: the regex function is working, but my regex is wrong, because your test returns true.

How about this example:


var reg = /[0-9]/;
alert(reg.test("2342 234 2sdg 234"));

This returns true for me as well! But this regex should ONLY take numbers, it's as simple as you can get.
It seems to me that I either don't understand the regex system here, or it's broken.

Am I getting something wrong? Thanks for your help :)

Daniil
Aug 29, 2014, 5:46 AM
I would try this.

var reg = /^[\d]+$/;
http://stackoverflow.com/questions/9011524/javascript-regexp-number-only-check