I'm trying to create a form with different inputs, input validation error receiving text, it does not validate (I want to be just text). The code is:
<form action="" id="myform" name="checkform" onsubmit="return validateform()" method="post">
<label for='Name'>Name:</label>
<input type="text" name="name" id="name"><br>
<label for='Surname'>Surname:</label>
<input type="text" name="surname" ><br>
<input type="submit" name="submit" value="Submit">
</form>
The javascript is:
function validateform(){
if(document.checkform.name.value == ""){
window.alert("Please insert your name.");
document.checkform.name.focus();
return false;
}
var nameText = document.forms["myform"]["name"].value; /* here is the problem :-( */
if (!name.match(/^\s*[A-Za-z]+\s*$/)) {
alert("Insert just letters !!!");
return false;
}
}
nameTextand then on the next line sayname.match()- it should benameText.match(). Why are you usingdocument.checkform.name.valuefirst and thendocument.forms["myform"]["name"].valueto reference the same field? Pick a syntax and stick with it.<input type="text" name="name" pattern="/[a-zA-Z]+/" required>(+ polyfill for non-html5 browsers)