0

I have the following Javascript code. It prevents the textbox from being empty, but I also need it to prevent numbers from being entered (I don't know how to add that)

if (document.getElementById('firstname').value=="") {
    errormessage+="Enter your first name \n";
    document.getElementById('firstname').style.borderColor="red";
} else {
    document.getElementById('firstname').style.borderColor="green";     
}
1

1 Answer 1

0

check the number input by regexp type /[0-9]/ with test function .That will return true when any 0 from 9 is exist.

var val = document.getElementById('firstname').value;
if (val =="" || /[0-9]/.test(val) == true){
 // error show
}
else{

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.