Reputation: 167
I have an input field for which I need to make sure the input is only and only characters from the alphabet, with no:
I want to do this using if else statement only. So far the code is:
HTML:
<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 == ""){
myform.action="firstnameerror.html";}
if (isNaN(x)) {
myform.action="lastname.html";
} else {
myform.action="firstnameerror1.html"
}
}
Upvotes: 1
Views: 4284
Reputation: 17351
You can check it using regular expressions (RegExp
), specifically with the .test()
function:
if(/[^a-zA-Z]/.test(x))
// code to throw error
This will run the code to throw an error if any non-alphabetic character is contained in x
.
Upvotes: 4