0

i want to validate a input by regex. if user enter any character with in a to z or A to Z then it will not accepted but if user enter + sign or bracket or any digit then it will be accepted. i use the regex but it did not work as per my requirement.

var regEx = '/^[A-Za-z]*$/';
            if (flag) {
                var val = jQuery.trim($("input[id*='txtfphone']").val())
                if (val.match(regEx)) {
                    if (_AdjustHeight == 0) {
                        $(".ui-dialog").animate({ height: '+=' + dialog_loader.height + 'px' }, 600, function () {
                            _AdjustHeight = 1;
                            $('#feed_loader').fadeIn('slow').html('<span>' + dialog_Msg.Invalid_Phone + '</span>');
                            $("input[id*='txtfphone']").focus()
                        });
                    }
                    else if (_AdjustHeight == 1) {
                        $('#feed_loader').fadeOut('slow', function () {
                            $('#feed_loader').html('<span>' + dialog_Msg.Invalid_Phone + '</span>');
                        }).fadeIn('slow');

                        $("input[id*='txtfphone']").focus()
                    }
                    flag = false;
                    return false;
                }
            }

so help me with right regex. it will be accepted if user enter data like

+913325806589
+91 33 25806589
913325806589
91 33 25806589
91 (33) 25806589
+91 (33) 25806589

but if user enter any alphabet as a input then it will not be accepted. like

aaab
+a913325806589
+91332a5806589
+91332a5806589b etc

a to z any character will not be accepted. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

0

does this one meets your need?

var regEx = /^\+?\d+[\d\s]*(\s*\(\d+\)\s*\d*)*$/ //no quote !!

for your

var regEx = '/^[A-Za-z]*$/'

you have defined the regEx to a string, so it won't take any effect.

leonhart
  • 1,193
  • 6
  • 12
  • i use your regex but postback occur i think there is some problem.here is my code where i use it.var regEx = '^\+?\d+[\d\s]*(\s*\(\d+\)\s*\d*)*$'; var val = jQuery.trim($("input[id*='txtfphone']").val()) if (!val.match(regEx)) { //not valid } what is wrong here? – Thomas Jun 28 '13 at 08:28
  • what's your `val()` of `input[id*='txtfphone']`? – leonhart Jun 28 '13 at 08:32
  • val() return txtfphone value whatever user input. so the variable val will contain the phone number like +913365209874 etc – Thomas Jun 28 '13 at 08:37
  • please check [here](http://regex101.com/r/xP0wL6), i think the "+913365209874" should be valid. what's the problem you got? – leonhart Jun 28 '13 at 08:43
  • the moment i put your regex then error occur. jquery is not giving the error message. – Thomas Jun 28 '13 at 08:46
  • @Thomas `var regEx = '^\+?\d+[\d\s]*(\s*(\d+)\s*\d*)*$'` is wrong, in this case regEx is just a string but not regex ,just `var regEx = /^\+?\d+[\d\s]*(\s*(\d+)\s*\d*)*$/` – leonhart Jun 28 '13 at 08:47
  • @Thomas updated your [fiddler](http://jsfiddle.net/terryleonhart/UGar9/2/), check again – leonhart Jun 28 '13 at 08:54
  • fiddler is not updated i checked. i use ur regex var regEx = '/^[A-Za-z]*$/' but found problem. when i enter abc in phone field them regex did not match. if it would match then i can prevent user to stop putting alphabet or alphanumeric value. if possible have a look. thanks for your effort. – Thomas Jun 28 '13 at 09:02
  • @Thomas i have updated the answer, have you checked it? your problem is you made your `regEx` to be a string, so it won't match anything. my fiddl is http://jsfiddle.net/terryleonhart/UGar9/2/ – leonhart Jun 28 '13 at 09:03
  • this time it was ok but + sign become mandatory. so please make it option because user may enter + sign or may not. thanks for effort. – Thomas Jun 28 '13 at 09:36
0

The regrex you are using is anchored at both the beginning and end, which means it will only match when the input contains only letters. It would not, for example, match "12a".

It also fails to check for all of the other invalid characters you haven't mentioned... !£#@ etc.

It would be better to use a regex that matches what you do want, and then see if the input fails to match.

From your description and examples, this should match valid input:

/^\s*\+?[\d\s]*(\([\d\s]+\)?)[\d\s]*$/

You then need to negate your match test:

if (!val.match(regEx)) {
knolleary
  • 9,777
  • 2
  • 28
  • 35
  • i use your regex but postback occur i think there is some problem.here is my code where i use it.var regEx = '/^\s*+?[\d\s](([\d\s]+)?)[\d\s]$/'; var val = jQuery.trim($("input[id*='txtfphone']").val()) if (!val.match(regEx)) { //not valid } what is wrong here? – Thomas Jun 28 '13 at 08:27
  • What input did you test with? – knolleary Jun 28 '13 at 08:37
  • the variable val will contain the phone number like +913365209874 etc and i am trying to validate it. – Thomas Jun 28 '13 at 08:37