Page 1 of 1

several input names

Posted: Tue Mar 22, 2011 8:04 am
by pedroz
Verifying a post form, if I have an input name the following code works perfectly: user clicking the submit, if submit_name is blank it will change the border color

if(jQuery("#submit_name").val()=="") {
jQuery("#submit_name").css('border-color','red');
return false;
}

How can I do it if there are several input form names?
<input type="name" id="submit_name1">
<input type="name" id="submit_name2">
<input type="name" id="submit_name3">
...
<input type="name" id="submit_name15">


Is there any similar thing with a for cycle?
Like

for ($i=1; $i++; $i < 16){
if(jQuery("#submit_name[$i]").val()=="") {
jQuery("#submit_name[$i]").css('border-color','red');
return false;
}


Thanks!!!
s

Re: several input names

Posted: Sun Mar 27, 2011 2:36 pm
by dgreenhouse
First thing... You've got the increment and loop-end test reversed.
The loop will run forever.

Try:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Color input's border with jQuery</title>
<script type="text/javascript" src="../includes/jquery/jquery-1.5.1.js"></script>
<script>
function borderCycle() {
  for ($i=1; $i < 16; $i++){
    if (typeof $("#submit_name"+[$i]) == 'undefined') {
      break;
	  }
    if($("#submit_name"+[$i]).val()=="") {
      $("#submit_name"+[$i]).css('border-color','red');
    }
  }
}
</script>
</head>

<body>
<input type="name" id="submit_name1" />
<input type="name" id="submit_name2" />
<input type="name" id="submit_name3" />
<input type="button" value="Cycle Borders" onclick="borderCycle();" />
</body>
</html>