Home > Enterprise >  why is this not validating select?
why is this not validating select?

Time:01-14

I want to validate my form that includes inputs, and one select , but at the moment when I want to validate does not works with my select , how can I do it or edit my code to validate all together, but I am having problems with it . any ideas.

$("#form1").submit(function(e){
e.preventDefault();
var letters1=/^[A-Za-z] $/;
var letters2 = /^[a-zA-Z0-9.!#$%&'* /=?^_`{|}~-] @[a-zA-Z0-9-] (?:\.[a-zA-Z0-9-] )*$/;
var email=  document.getElementById("email").value;
var password=  document.getElementById("password").value;
var fname=document.getElementById("fname").value;
var lname=document.getElementById("lname").value;
var selectUser=document.getElementById("user");
var valid=selectUser.value=="select user";

if(email.length>0 && email.match(letters2))
{
    if(password.length>0)
    {
        if(fname.length>0&&fname.match(letters1))
        {
            if(lname.length>0&&lname.match(letters1))
            {
                if(selectUser.invalid)
                {
                    alert('please enter user');
                    selectUser.className='error';
                }
                else{
                selectUser.className='';
                }
            }
            else{
                alert("wrong last");
            }
        }
        else{
                alert("wrong first");
            }
    }
    else{   
                document.getElementById("password").style.borderColor="red";
                alert("wrong password");
            }
}
else{
                alert("wrong email");
            }

return !invalid;})

html

<select id="user" name="user">
                <option value="select user">select user</option>
                <option>Admin </option>
                <option> SuperAdmin</option>
                <option> Staff</option>
            </select>
            <div >
            <a href="#submit1">
                <button type="submit">Submit</button></div>
            </a>
        </div>

CodePudding user response:

You have declared a variable with the name ‘valid’. But you are not using it.

You should check ‘valid’ instead of ‘selectUser.invalid’.

Also the condition for ‘valid’ should be the opposite: The select’s value should not be ‘select user’

  •  Tags:  
  • Related