parsley.js telephone digits input validating with spaces
Asked Answered
H

2

7

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?

Howell answered 12/4, 2014 at 15:2 Comment(0)
E
17

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!

Electroform answered 16/4, 2015 at 12:12 Comment(1)
Your pattern allows multiple occurrence of characters like . ( ) - and so on. A typical phone number may not have multiple occurrence of those characters or at least not the consecutive one.Arrowroot
A
4

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)

Abdullah answered 26/4, 2014 at 20:27 Comment(1)
I have multiple patterns to check, how can i do that ?Chapfallen

© 2022 - 2024 — McMap. All rights reserved.