The easiest way to do this is to simply use a few fields:
<div>
Phone:
(<input type="text" name="phone-1" maxlength="3">)
<input type="text" name="phone-2" maxlength="3">-
<input type="text" name="phone-3" maxlength="4">
</div>
<div>
SSN:
<input type="text" name="ssn-1">-
<input type="text" name="ssn-2">-
<input type="text" name="ssn-3">
</div>
Although this approach is, of course, simple, it is not great. The user must click a tab or click on each field to enter data, and there is nothing (except common sense) that prevents them from entering things other than numbers.
, , , , . , , , , . , "5555551212" "(555) 555-1212", "5555551212".
, HTML5 ( SSN). , , , .
, :
<div>
<label for="fieldPhone">Phone: </label>
<input type="tel" id="fieldPhone" placeholder="(555) 555-1212">
</div>
<div>
<label for="fieldSsn">SSN: </label>
<input type="text" id="fieldSsn" name="ssn" placeholder="555-55-5555" pattern="\d{3}-?\d{2}-?\d{4}">
</div>
, . , polyfill. HTMl5:
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills#wiki-web-forms
, , , - .
; , , , , , . , . , jQuery:
$('form').on('submit', function(){
$(this).find('input[type="tel"]').each(function(){
$(this).val() = $(this).val().replace(/[\s().+-]/g, '');
});
$(this).find('input[name="ssn"]').each(function(){
$(this).val() = $(this).val().replace(/-/g, '');
});
});
: , , , , .
- AJAX .serialize; , . , , , .
, . HTML5 , , . , "+1 (555) 555-1212", 8 7. 7 , :
/^\(?\d{3}\)?[.\s-]?\d{3}[.\s-]\d{4}$/
(, , , ) - 7- .
jsfiddle HTML5:
http://jsfiddle.net/Cj7UG/1/
, !