Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu June 03, 2023 . #HowTo . #Input

10 digit mobile number validation in javascript

In this article, I will show you how to validate 10 digit mobile number in javascript. input type="email" is enough to validate an email address. But to validate 10 degit mobile number, we have to add a condition in JavaScript using if statement.

We have a form and when the form is submitted, a JavaScript function is invoked.

Validation for 10 degit mobile number
<form class="event.preventDefault(); validate();">
  <label>Mobile Number: </label><input type="number" id="my-input" placeholder="91#######" value="">
  <input type="submit" value="submit">
</form>
<script>
  function validate() {
    const myInput = document.querySelector('#my-input');
    if(myInput.value.length != 10) {
      alert('10 digit mobile number is required');
    }
  }
</script>
Try it Yourself »
  1. event.preventDefault() is a JavaScript method that prevents the form from refreshing the current page. Read More
save
listen
AI Answer
1 Answer
  1. https://www.3schools.in/p/embed.html?q=Cjxmb3JtPgogPGxhYmVsIGZvcj0icGhvbmUiPkVudGVyIGEgMTAtZGlnaXQgcGhvbmUgbnVtYmVyOjwvbGFiZWw+PGJyPgogPGlucHV0IHR5cGU9InRleHQiIGlkPSJwaG9uZSIgbmFtZT0icGhvbmUiIHBhdHRlcm49IlswLTldezEwfSIgcmVxdWlyZWQ+PGJyPgogPHNtYWxsPkZvcm1hdDogMTIzNDU2Nzg5MDwvc21hbGw+PGJyPjxicj4KIDxpbnB1dCB0eXBlPSJzdWJtaXQiIHZhbHVlPSJTdWJtaXQiPgo8L2Zvcm0+
    In this example, the pattern attribute is used to specify a regular expression pattern that ensures exactly 10 digits are entered. The [0-9] matches any digit, and {10} specifies that there should be exactly 10 occurrences of the preceding pattern. The required attribute makes sure the input field cannot be submitted empty. The small element provides a hint on the expected format.
    Reply Delete
    Share
Write Your Answer
loading
back
View All