-4
</head> 

<body> 

        <fieldset id="user-details">    

            <label for="name">Name:</label>
            <input type="text" name="name" value="" /> 

            <label for="email">Email:</label> 
            <input type="email" name="email" value=""  /> 

            <label for="phone">Phone: (Ex:(xxx)xxx-xxxx)</label>
            <input type="text" name="phone" value=""  />

            <label for="zipcode">Zip-Code</label>
            <input type="text" name="zipcode" value="" />

        </fieldset><!--end user-details-->

        <fieldset id="user-message">

            <label for="message">Message:</label> 
            <textarea name="message" rows="0" cols="0"></textarea> 

            <input type="submit" value="Enviar" name="submit" class="submit" />     

        </fieldset><!-- end user-message -->

    </form>

</body> 
</html>

Hi guys, so i need to divide phone into 3 squares, i think this is called regular expression and i really need help, for example (xxx) xxx xxxx , phone number should be like this!

I would appreciate you help thanks!!

Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    Regular expressions are very useful, and I would suggest learning them if you haven't. Check [this site](http://www.regular-expressions.info/tutorial.html) for a tutorial. – C.B. Nov 15 '13 at 14:54
  • please post only code relevant to your question – Beat Nov 15 '13 at 14:55
  • 3
    Why are you posting a mile's worth of pointless HTML/css when you seem to be asking how to write a regex in PHP? – Marc B Nov 15 '13 at 14:55
  • Important: Not all phone numbers follow the `(xxx) xxx xxxx` format. If your site is based in the US and you know you'll never deal with any foreigners, then that's fine, but if you want your site to be usable to anyone outside of the US, you should avoid having a fixed format phone number field. The same applies to Zip codes too, by the way. – Spudley Nov 15 '13 at 14:59
  • @Beat Im Sorry, im new to the site, and this is my first question!, thanks for your suggestion – Fredy Montoya Nov 15 '13 at 15:24

3 Answers3

3

There is no standard regular expression for phone number. For your format regular expression is as follows:

       $PHONE_REGEX = /^([0-9]{3}) [0-9]{3} [0-9]{3}$/
       or
       $PHONE_REGEX = /^(\d{3})\s\d{3}\s\d{3}$/

In HTML5

     <input type='tel' name='phone' />  //This may not match the your pattern but easy to validate filed on client side
Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44
1
\(\d{3}\)\s\d{3}\s\d{4}

This should work with this format (xxx) xxx xxxx. I didn't test it.

Kevin Cittadini
  • 1,449
  • 1
  • 21
  • 30
0

You do not need a regular expression for this. Just give the user 3 squares:

<label for="phone">Telefono: (Ej:(xxx)xxx-xxxx)</label>
(<input type="text" name="phoneareacode" value="" maxlength="3" />) <input type="text" name="phoneexchange" value="" maxlength="3" /> - <input type="text" name="phonenumber" value="" maxlength="4" />
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77