views:

48

answers:

2

Hello, By using the pattern from here, I want to check while a user fills in the #id_phone text input field, if it is matching to the pattern, use the value of #id_phone to fill #id_new_phone's value. This doesn't seem to work for me.

$('#id_phone').change(function() {
    var myPattern = new RegExp(^0\(5\d{2}\) \d{3} \d{2} \d{2}$);
    var myStr = $("#id_phone").val();
    if ( myStr.match(myPattern) ){
        $('#id_new_phone').val(myStr);
    }
});

What can be the problem ?

+3  A: 

var myPattern = new RegExp("^0(5\d{2}) \d{3} \d{2} \d{2}$");

You should quote the pattern when using RegExp();

Also it's not a valid regexp because if you don't want to quote it, you should use the regexp construct. E.g.:

var myregex = /<some pattern here>/g
bisko
+5  A: 

You need to put (^0\(5\d{2}\) \d{3} \d{2} \d{2}$) inside quotes, and also need to escape backslashes, if you are about to use RegExp, instead of / /

"^0\\(5\\d{2}\\) \\d{3} \\d{2} \\d{2}$" 

And myPattern.test(myStr) is more preferable than using .match, because .match return a array which you don't really need that.

S.Mark