Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript validate select dropdown

Tags:

javascript

i have a form with javascript validation. there are 3 drop-down(select) fields with questions and 3 input fields with answers.

is there any way to validate the select fields so that they don't have the same question?

here is my code

<script type="text/javascript">
var errmsg;
function validate()
{
var textA= document.getElementById("text1");
var textB= document.getElementById("text2");
var textC = document.getElementById("text3");
var textD = document.getElementById("text4");
var textE = document.getElementById("text5");
var textF = document.getElementById("text6");
var txt1 = document.getElementById("text1").value;
var txt2 = document.getElementById("text2").value;
var txt3 = document.getElementById("text3").value; 
var txt4 = document.getElementById("text4").value;
var txt5 = document.getElementById("text5").value;
var txt6 = document.getElementById("text6").value;
var txt1_len = txt1.length;
var txt2_len = txt2.length;
var txt3_len = txt3.length;
var txt4_len = txt4.length;
var txt5_len = txt5.length;
var txt6_len = txt6.length;

if(txt1_len == '')
{
    errmsg = "Please select a question";
    document.getElementById("ermsg").innerHTML = errmsg;
    textA.focus();
    return false;
}

else if(txt2_len == 0 || txt2_len > 23 || txt2_len < 3)
{
    errmsg = "Invalid Answer";
    document.getElementById("ermsg").innerHTML = errmsg;
    textB.focus();
    return false;
}

else if(txt3_len == '')
{
    errmsg = "Please select a question";
    document.getElementById("ermsg").innerHTML = errmsg;
    textC.focus();
    return false;
}
else if(txt4_len == 0 || txt4_len > 23 || txt4_len < 3)
{
    errmsg = "Invalid Answer";
    document.getElementById("ermsg").innerHTML = errmsg;
    textD.focus();
    return false;
}
else if(txt5_len == '')
{
    errmsg = "Please select a question";
    document.getElementById("ermsg").innerHTML = errmsg;
    textE.focus();
    return false;
}

else if(txt6_len == 0 || txt6_len > 23 || txt6_len < 3)
{
    errmsg = "Invalid Answer";
    document.getElementById("ermsg").innerHTML = errmsg;
    textF.focus();
    return false;
}
else
{
    return true;

}

return false;
}


</script>

and then the html code

https://jsfiddle.net/johnmathew21/ty999fkv/

like image 353
johnmathew21 Avatar asked Nov 09 '22 03:11

johnmathew21


1 Answers

you should compare the text inside each ddl , i add the following condition :

 $('#text1 option:selected').text() === $('#text2 option:selected').text()

working fiddel : here

like image 185
Hasan Daghash Avatar answered Nov 15 '22 12:11

Hasan Daghash