简体   繁体   中英

jQuery Validation Add Method Regex Number with Parentheses

I'm using jquery validate plugin with a input field which ask to put into it a phone number and I want to add a validator method with a regex that'll valid any numbers starting with " 9 or 09 or +639 or 639 or (+63) or (+63) 9 " and 9 digit after it

jQuery.validator.addMethod("phoneno", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, "");
        return this.optional(element) || phone_number.length > 9 && 
        //phone_number.match(/^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/);
        phone_number.match(/^(9|09|\+639|639|\((\+63)\) 9)\d{9}$/);
      }, "<br />Please specify a valid phone number");

This code works and can valid any number started with +63 , 9 , 09 , 63 except when it start with (+63) or (+63) 9 As an example, it valid +639123456789 639123456789 9123456789 but not with Parentheses

I think that there is a mistake in the match regex

^(9|09|\\+?639|\\(\\+63\\)\\s?9?)\\d{9}$

This validates :

+639123456789
9123456789
(+63)9123456789
(+63) 9123456789
(+63)123456789
(+63) 123456789

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM