-1
<script>
function lettersOnly(input) {
var regex = /[^a-z]/gi;
input.value = input.value.replace(regex, "");
}
</script>

<input type="tel" name="Contact number" pattern="\d{8}" title='8 digits telephone number' placeholder=" Contact Number" id="contactno"
            onkeyup="lettersOnly(this)"><br>   

The input section is part of a form and I want to make sure only numbers can be typed into the contact number input.

  • Is this a question or the answer? – Tomas Dittmann Jul 31 '16 at 15:34
  • Its a question, not answer – Aung Thura Zaw Jul 31 '16 at 15:35
  • @AungThuraZaw Check out the dupe. Please search before posting. – Praveen Kumar Purushothaman Jul 31 '16 at 15:36
  • Look, I am just a student, alright, so I am really bad, I am just needing help so forgive me if I am not smart – Aung Thura Zaw Jul 31 '16 at 15:38
  • 1
    @PraveenKumar Note, duplicate Answer provides jQuery solution where neither original Question nor present Question includes jQuery tags. Note also that duplicate Answer also allows non-digit to be input and displayed before removing non-digit character – guest271314 Jul 31 '16 at 15:41
  • 2
    @AungThuraZaw Okay, you are not that bad. LoL. No one said you bad, but please do a search. That's what is the request. – Praveen Kumar Purushothaman Jul 31 '16 at 15:44
  • 1
    For phone numbers it's a *terrible* idea to restrict input to only numeric characters. How would someone indicate an extension? How would someone indicate that `#` must be used? You're asking for generally 10 digits to be entered, and the user can't even include a space or hyphen to make it easier to verify that they input their number correctly? The general answer is to strip the unnecessary characters *after* the user has submitted the data. – zzzzBov Jul 31 '16 at 16:48

1 Answers1

0

You can use oninput event, RegExp /\D/ to check if input is not a digit character, String.prototype.replace(); if typed character is not a digit set .value to characters before non-digit

document.querySelector("input")
.oninput = function(e) {
  e.target.value = e.target.value.replace(/\D/g, "")
}
<input type="text" />
guest271314
  • 1
  • 15
  • 104
  • 177