Sophia111 Sophia111 - 7 months ago 23
HTML Question

Check if input is only alphabets in javascript using IF ELSE statement

I have an input field for which I need to make sure the input is only and only characters from the alphabet, with no:

  • numbers

  • special characters

  • number + alphabet combinations

I want to do this using if else statement only. So far the code is:


<form id="myform" onsubmit="return check();" >
<input class="input" type="text" name="firstname">

And JavaScript is:

function check() {
var x = document.forms["myform"]["firstname"].value;
if (x == null || x == ""){
if (isNaN(x)) {
} else {


You can check it using regular expressions (RegExp), specifically with the .test() function:

    // code to throw error

This will run the code to throw an error if any non-alphabetic character is contained in x.