Reputation: 3913
if (standardFlag == "ASCE")
{
if (document.querySelectorAll('#Ss_param, #S1_param, #T0_param, #TL_param').value == "" ){
alert("Please fill out all the required fields before generating your design spectrum");
}
else if (document.getElementById('Ss_param').value < document.getElementById('S1_param').value {
alert("S1 must be less than Ss!");
}
I'm trying to check whether either Ss_param, S1_param, T0_param or TL_param input ids hold any value. The second condition passes its own case which is how I know that the entire conditional is not faulty, but the first condition doesn't pass when it should. How do I rectify this?
Upvotes: 0
Views: 229
Reputation: 32921
First and foremost: You should just look into a validation library.
If you'd like to still approach it this way, I'd look into jQuery's filter
method.
This would return only elements that have a value:
var $elementsWithValue = $('#Ss_param, #S1_param, #T0_param, #TL_param').filter(function(){
return this.value.length > 0
});
Then you can just check $elementsWithValue length.
Upvotes: 2
Reputation: 16092
After reformatting your code to be more readable, it appears you're missing an ending close brace }
, is this a typo in this question, or an error in your code?
Additionally your second condition is missing a closing parenthesis )
. Did you notice javascript errors when you open up the console in the browser you're testing on?
Once you solve that problem, you need to be aware that you're using native browser functions. When you use querySelectorAll
you get an array of objects back. Calling .value
on them will probably also be causing a javascript error. You need to loop through each value, or use jQuery (which will arguably give you a more maintainable solution).
Using jQuery, this will give you the desired result:
if (standardFlag == "ASCE")
{
var error = false;
$.each(["Ss_param", "S1_param", "T0_param", "TL_param"], function(){
if ($("#" + this).val() == ""){
error = true;
alert("Please fill out all the required fields before generating your design spectrum");
}
})
if(!error && $("Ss_param").val() < $("#S1_param").val())
alert("S1 must be less than Ss!");
}
}
Upvotes: 1
Reputation: 92893
You can't get one value from multiple elements. Just compare them one at a time:
if (document.getElementById('Ss_param').value === ""
|| document.getElementById('S1_param').value === ""
|| document.getElementById('T0_param').value === ""
|| document.getElementById('TL_param').value === "" ){
(And check your JavaScript console for syntax errors. Your else if
test is lacking a closing parenthesis.)
Upvotes: 1
Reputation: 44740
You can use filter if your id's always end's with _param
if ($('[id$="_param"]').filter(function(){
return $(this).val() === "";
}).length > 0){
alert("Please fill out all the required fields before generating your design spectrum");
}
Upvotes: 1