- I have a text box validation here. The error message will not come as an alert, instead it will get printed in the html div tag.
Html:
<div id="error" style="position:absolute; left:auto; top:7px;"></div>
<div style="position:absolute; left:auto; top:25px;">
First name: <input type="text" id="fname" name="fname"><br>
Last name: <input type="text" id="lname" name="lname"><br>
<input type="submit" value="Submit" onclick="requiredFields()"><div>
Javascript:
function requiredFields(){
var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
if(fName == ""){
document.getElementById("error").innerHTML = "First name field cannot be empty";
}else if(lName == ""){
document.getElementById("error").innerHTML = "Last name field cannot be empty";
}else{
document.getElementById("error").innerHTML = "";
alert("successful");
}
}
- On click of a button, am printing the error message.
- My question is, is there any live validation in javascript?
- I mean, at first the error message should come at, button on click, and the time user enters value into text box, if first name text box has any value, i need to clear the error message without clicking the button again.
just check my link and help me.. http://jsfiddle.net/GACKm/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…