user16159484
user16159484

Reputation: 1

How to validate a phone number doesn't contain any other characters including - and . using javascript in a html page, shouldnt be less than 10 digits

This is in Adobe campaign classic.

type="text" maxlength="10" pattern="[0-9]{10}"

It is allowing only 10 digits, but accepting - and . I want the user to enter only 10 not less or more and accept only digits. Please help.

Upvotes: 0

Views: 165

Answers (2)

Adrian Sanchez
Adrian Sanchez

Reputation: 171

You can always sanitize the input using a javascript piece of code. But as you are asking for a validation, simply use a numeric HTML input with max and min attributes.

E.g.

<input type="number" min="1000000000" max="9999999999">

Upvotes: 1

Hao Wu
Hao Wu

Reputation: 20669

What you could do is to replace non-numeric characters(\D) after user input:

[...document.querySelectorAll('input[type="tel"]')].forEach(i =>
  i.addEventListener('input', () => i.value = i.value.replace(/\D/g, ''))
);
<input type="tel" maxlength="10" pattern="\d{10}" />

Upvotes: 0

Related Questions