jquery validation regex for digits with hyphens
Can someone provide开发者_开发问答 a regex that only allows digits and optionally hyphens
I have got as far as phone_number.match(/[0-9-]/); but with no luck
The following matches numbers interspersed with dashes, but not double dashes:
/^\d+(-\d+)*$/
What you're describing is used in the jquery validate plugin http://docs.jquery.com/Plugins/Validation/CustomMethods/phoneUS
^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$
I came here searching for something similar and found what I needed at http://regexlib.com/ they have a great collection of expressions as well as a helpful cheat sheet.
Try this plugin: Masked Input jQuery plugin. It allows to make own masks and simplifies input for user.
/^\w*-?\w*-?\w*$/
- \w* Alphanumeric character repeated 0 or many times.
- -? Optional hyphen.
Try this out...
/^[0-9]+(\-[0-9]+)*$/
Not phone numbers, but what you wanted from the comments
Check this out...
var phoneno = /^+?[0-9]+(-[0-9]+)*$/;
Ans +954-555-1234
Here is the solution for a input with hyphens
$('input[type="tel"]').keyup(function() {
this.value = this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="tel"></input>
精彩评论