Reputation: 307
i'm trying to validate a form using a simple javascript script however it is not working, can anyone tell me what is wrong with my code? (sorry for my vague question)
JS code:
<script type="text/javascript">
function validation()
{
var fName=document.forms["teacherReg"]["firstname"].value;
var lName=document.forms["teacherReg"]["lastname"].value;
var uName=document.forms["teacherReg"]["username"].value;
var pWord=document.forms["teacherReg"]["password"].value;
var cPWord=document.forms["teacherReg"]["confirmpassword"].value;
if (fName="" || lName="" || uName="" || pWord="")
{
alert("Not a valid entry");
return false;
}
}
</script>
html form:
<form name="teacherReg" action="" method="POST" onsubmit="return validation();">
1. First name:
2. Last name:<br/><input type="text" name="firstname" id="firstname" />
<input type="text" name="lastname" id="lastname" /><br/><br/>
3. Desired Username:
<br/><input type="text" name="username" id="username" /><br/><br/>
4. Desired Password:
5. Confirm Password:<br/><input type="password" name="password" id="password" />
<input type="password" name="confirmpassword" id="confirmpassword" /> <br/><br/>
<center><input type="submit" value="Register" name="submitbutton" class="button" /></center>
</form>
I expect it to return false if any of the fields "fName, lName, uName, pWord" are blank, however it is always returning true
Upvotes: 0
Views: 852
Reputation: 17997
Please try to use the ID instead of the form object.
function validation(){
var username = document.getElementById('username').value;
// etc.
}
Upvotes: -3
Reputation: 2002
This is the problem:
if (fName=="" || lName=="" || uName=="" || pWord=="")
Upvotes: 1
Reputation: 8400
=
is assignment operator , where ==
is comparison operator. use ==
for comparing values.
Upvotes: 0
Reputation: 193261
The problem is that you confused =
(assignment operator) operator with ==
(comparison operator):
if (fName = "" || lName = "" || uName = "" || pWord = "") {
It should be
if (fName == "" || lName == "" || uName == "" || pWord == "") {
Upvotes: 5