Welcome Guest, Not a member yet? Register   Sign In
Help in jquery loop
#1

[eluser]Techno Heart[/eluser]
Hi all,

I used a php for loop to create a block in html page.the block has six fields.in javascript page i have to validate that block.(i.e) if any one field among six is empty then it should show alert message.i have written conditions as follows
Code:
var from_month = $("select[id='from_month']").map(function(){return $(this).val();}).get();
var to_month = $("select[id='to_month']").map(function(){return $(this).val();}).get();
var from_year = $("input[id='from_year']").map(function(){return $(this).val();}).get();
var to_year = $("input[id='to_year']").map(function(){return $(this).val();}).get();
var designation = $("input[id='designation']").map(function(){return $(this).val();}).get();
var company = $("input[id='company']").map(function(){return $(this).val();}).get();
the above are the six fields which has multiple values (ie) array.

now i am writing condition as
Code:
for(var i=0;i<3;i++)
{
if(from_month[i] != 'Month' && to_month[i] == 'Month' && from_year[i] == 'Year' && to_year[i] == 'Year' && designation[i] == 'Designation' && company[i] == 'Company')
{ alert("Enter all details for Experience "+(i+1));
break;
}
else if(from_month[i] != 'Month' && to_month[i] != 'Month' && from_year[i] == 'Year' && to_year[i] == 'Year' && designation[i] == 'Designation' && company[i] == 'Company')
{ alert("Enter all details for Experience "+(i+1));
break;
}
else if(from_month[i] != 'Month' && to_month[i] != 'Month' && from_year[i] != 'Year' && to_year[i] == 'Year' && designation[i] == 'Designation' && company[i] == 'Company')
{
alert("Enter all details for Experience "+(i+1));
break;
}
etc
}

if i write conditions like this the number of lines of code is more.so any one have idea to make it simple using same loop.?
#2

[eluser]Flemming[/eluser]
could you give us a 'bigger picture' of what you're doing? Is it client-side validation?
#3

[eluser]danmontgomery[/eluser]
jQuery uses CSS style selectors, there's no reason to do:

Code:
$("select[id='from_month']")

When you can do:

Code:
$("select#from_month")
// or just:
$("#from_month")

I would just give a class like "required" to each element and check the value of all required fields

Code:
$('.required').each( function() {
    if($(this).val() == null) {
        alert('Enter all details for Experience');
    }
});
#4

[eluser]Techno Heart[/eluser]
Yes it is client side validation.Actually in html using php for loop i created 3 div which has six fields each.using jquery i got those values as array as given above.
the text box values by default will be their label names i.e(Month,Year.etc).So if user fills any one field in on div block and if he try to submit the form then it should show alert.How to validate other than the code i given above.i want simple way.

using each loop also not possible.
#5

[eluser]Flemming[/eluser]
have you looked a jquery.validate()?

http://docs.jquery.com/Plugins/Validation/validate

this in my opinion is the best way to do any client side validation




Theme © iAndrew 2016 - Forum software by © MyBB