parsley.js telephone digits input validating with spaces

旧城冷巷雨未停 提交于 2020-01-02 03:51:29

问题


I have an input for telephone number.

I would like to write this format: 0175 6565 6262 (with spaces). But if write with " " spaces so get error and I write without spaces so get not error.

Here my HTML Input:

<input type="text" data-parsley-minlength="6" data-parsley-minlength-message="minlength six number" data-parsley-type="digits" data-parsley-type-message="only numbers" class="input_text" value="">

Hope someone can help me?


回答1:


That's a great answer, but it's a bit too narrow for my needs. Input field should be tolerant of all potential inputs – periods, hyphens, parentheses, spaces in unexpected places, plus signs for international folk – and using this document from Microsoft detailing what numbers IE11 should accept, I've come up with this:

data-parsley-pattern="^[\d\+\-\.\(\)\/\s]*$"

Every number in that list passes the test with flying colours. Enjoy!




回答2:


If you want your input to accept a string like "nnnn nnnn nnnn" you should use a regular expression. For example, you can use the following HTML:

<input type="text" name="phone" value="" data-parsley-pattern="^\d{4} \d{4} \d{4}$" />

With this pattern the input will only be valid when you have fourdigits«space»fourdigits«space»fourdigits You can test or tweak the regular expression and test it here: http://regexpal.com/

If you will use this pattern multiple times in your project I suggest you create a custom validator (see http://parsleyjs.org/doc/index.html#psly-validators-craft)



来源:https://stackoverflow.com/questions/23032040/parsley-js-telephone-digits-input-validating-with-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!