-1

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:
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    2. Last name:<br/><input type="text" name="firstname" id="firstname" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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:
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    5. Confirm Password:<br/><input type="password" name="password" id="password" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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

4
  • You shouldn't use UPPERCASE HTML (<SCRIPT>) Commented Feb 14, 2014 at 9:11
  • 1
    You should tell us what did you expect and what happened? why do you think it is not working? etc.. Commented Feb 14, 2014 at 9:11
  • @Vadorequest you shouldn't html is not case sensetive Commented Feb 14, 2014 at 9:12
  • @user689 edited in question Commented Feb 14, 2014 at 9:13

5 Answers 5

5

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 == "") {

Fixed demo: http://jsfiddle.net/49xDH/

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

Comments

1

This is the problem:

 if (fName=="" || lName=="" || uName=="" || pWord=="")

Comments

0

= is assignment operator , where == is comparison operator. use == for comparing values.

Comments

0

Use the comparison operator == instead of the assignment operator =.

2 Comments

you shouldn't duplicate an already given answer without any added value
Yeah by the time i started typing ,an answer was posted .Its not my fault.Sorry bro..
-3

Please try to use the ID instead of the form object.

function validation(){
    var username = document.getElementById('username').value;
    // etc.
}

3 Comments

It also looks like your validation is wrong. if (!fName || !lName || !uName || !pWord)
@Vadorequest : This answer has nothing to do with the problem
It's better to use the ID than the document form. Clean. His code was badly indented, hard to understand and wasn't following stackoverflow presentation rules.

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.