So basically I have created input fields in HTML for a persons first and last names. I want this field to limit their inputs to only text and give them an alert otherwise using vanilla JS please.
Here is the HTML code:
<div>
First Name:<br> <input id="name" type="text" placeholder="Type Here" pattern="[A-Za-z]"><br>
Last Name:<br> <input id="name" type="text" placeholder="Type Here" pattern="[A-Za-z]">
</div>
As you can see I previously tried to limit the input by using the pattern="" method I saw in another post but couldn't get it to work. Advice on that would be much appreciated as well.
And here is my JS function as it stands now:
var nameinput=document.getElementById('name').value;
var input=function(field){
if(isNaN(field)===false || field===""){
alert('Please Enter Your Name');
return false;
}
}
input(nameinput)
It's probably something obvious. I'm pretty new to JS but any help would be greatly appreciated!
patternattribute works on form + submit I think.