Monday 15 February 2010

PHP preg_match regex not working -


I am trying to validate a field for US phone numbers and if the information entered in a field is not valid . It is currently not accepting any information as entered as valid even if regex is valid:

  add_action ('register_form', 'myplugin_register_form2'); Function myplugin_register_form2 () {$ phone_number = (isset ($ _POST ['phone_number']))? $ _POST ['phone_number']: ''; ? & Gt; & Lt; P id = "phone_number" & gt; & Lt; Label for = "phone_number" & gt; & Lt ;? Php _e ('phone number & lt; font size = "1" & gt; (XXX XXX XXX) & lt; / font & gt;', 'mydomain')? & Gt; & Lt; Br / & gt; & Lt; Input type = "text" name = "phone_number" id = "phone_number" class = "input" size = "25" style = "text-line: true" maximum length = "14" /> & Lt; / P & gt; & Lt ;? Php} // 2 Phone_Number requirement add_filter ('registration_errors', 'myplugin_registration_errors2', 10, 3); Function myplugin_registration_errors2 ($ errors, $ sanitized_user_login, $ user_email) {$ pattern = '/ ^ (?: 1 (?: [. -])?)? (?: \ ((? = \ D {3} \)))? ([2-9] \ d {2}) (? (& Lt; = \ (\ d {3}) \))? ? (? (& Lt; = \ d {3}) [.-]) ([2-9] \ d {2}) [? -]? (\ D {4}) (?: (? I: ext) \ ?? (\ D {1,5})) $$ / '; If (Preg_match ($ pattern, $ phone_number, $ matches)) {$ errors- & gt; Add ('phone_number_error', __ ('& lt; strong & gt; ERROR & lt; / strong & gt;: You must include a valid phone number.', 'Madmen')); Return $ errors; } If (empty ($ _POST ['phone_number'])) $ errors- & gt; Add ('phone_number_error', __ ('& lt; strong & gt; ERROR & lt; / strong & gt;: You must include a valid phone number.', 'Mydomain')); Return $ errors; }    

OK, I found one that does not throw any errors:

  / ^ (? :( :(?: 1 [\ / \ s -.]?) ((((? (?)?):: ?? \ ((= \ d {3} \) )) ([(| (| 37 | 96)) [2-9] [0-8] [0-9] (<(11)) ??!?!) [2-9]) (?? \ (? (& Lt; = \ (\ d {3})?) - ([0-9] {2} (  < P> But you must first strip all the numbers:  
  $ input_number = preg_replace ("[^ 0- 9]", "", $ "$ input_number");   

You can also format your number with They are:

  preg_match ($ pattern, $ input_number); $ number = $ result [1]. "$ Result [4]. $" $. "Result [6];   

There is another way on which I have set the input mask with jQuery described in the thread mentioned above. The legitimate phone number.

I did not find any good regexp Matches

No comments:

Post a Comment