4

I'm currently having an input element for a phone number and trying to use the pattern attribute but it refuses to do so. it says "Validation(HTML5): Pattern is not a valid attribute of element input"! When I change the type to "text" it says that pattern attribute is only valid when title is present!

<input type="number" class="form-control"data-require="" id="Mobile" placeholder="Mobile No" autocomplete="off" pattern="[\+]\d{3}d{9}" required>

UPDATE:

I Added title attribute and it's working now! but my only issue is that when i click submit, it submits the form even though that the format is not matching.

3
  • Well did you try to add title attribute? Commented Feb 28, 2018 at 8:15
  • i updated my question, please check Commented Feb 28, 2018 at 8:16
  • For your example: [\+]\d{3}d{9} did you mean d or \d? Also, if the type is number, what's the goal here? To ensure the length? And why not just have \d{12} ? Commented Feb 28, 2018 at 8:33

3 Answers 3

5

The <input> should be valid without the title attribute (validated on https://validator.nu/):

<!DOCTYPE html>
<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <input type="text" class="form-control" data-require="" id="Mobile" placeholder="Mobile No" autocomplete="off" pattern="[\+]\d{3}d{9}" required>
  </body>
</html>

Additional changes:

  • A space is missing before attribute data-require.
  • The pattern attribute is only allowed on the following types: email, password, search, tel, text, or url.

Your regular expression ([\+]\d{3}d{9}) is also invalid. You can try one of the following rules:

  • [\+]\d{3}\d{9}
  • [\+]\d{12}

You are missing the \ before the second d to match only numbers. The second pattern is a minified version of the first pattern.

Sign up to request clarification or add additional context in comments.

5 Comments

the pattern i wrote is for the number to be in the format : +997566666666. can't this be used internationally?
you know you miss the \ on the second d
@SebastianBrosch - not to mention that if it should be \d it simply checks that it's 12 digits long.
Since you are using an input of type number, you could also just set the min value to 100000000000 and the max value to 999999999999 to ensure it is 12 digits long.
And the + character?
0

It's fixed, just added this in my javascript.

/[+]\d{3}d{9}/.test(PhoneNo)

Comments

-1

You can add oninvalid attribute to the tag

<input type="text" name="HasAPattern" pattern="\w{3}" title="Enter 3 characters" oninvalid="setCustomValidity('Needs to match patern')" >

The HTMLSelectElement.setCustomValidity() method sets the custom validity message for the selection element to the specified message. Use the empty string to indicate that the element does not have a custom validity error.

You can also use code like event.preventDefault(); inside it to cancel the submit if invalid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.