views:

222

answers:

5

If I have multiple textboxes which have different ID's but all id's start with 'pics' preceeded by a number. so:

textbox1  id = 'pics1' value='test1'
textbox2  id = 'pics2' value='test2'
textbox3  id = 'pics3' value='test1'
...
submit

I want to do some validation that when the user clicks submit..there are no two values in the textboxes that are same. For the above example, validation will result in error and form wont submit. because pics1 and pics3 have same values.

A: 

Selectors/attributeStartsWith

andres descalzo
A: 

Try this:

<script>
function validate()
{
    var areas = document.getElementsByTag('textarea');
    for (var i=0; i<areas.count;i++)
        for (var j=i+1; j<areas.count;i++)
            if (areas[i].id.startsWith('pics') && areas[i].id.startsWith('pics') && areas[i].value == areas[j].value)
                return false;
    return true
}
</script>

<form onsubmit="return validate();">
    <textarea id = 'pics1'></textarea>
    <textarea id = 'pics2'></textarea>
    <textarea id = 'pics3'></textarea>
    <input type="submit" />
</form>
Veton
this is not good as I will have to create a var for each element
+1  A: 

One solution (not tested):

function validate() {

   var isValid = true;

   $("input:text[id^=pics]").each(function() {
      if ($("input:text[id^=pics][value=" + $(this).val() + "]").length > 1)
        isValid = false;
      });

   return isValid;
}
Philippe Leybaert
+1, love the idea!
karim79
A: 

This will work for however many text inputs that you have

function validate()  {
  var inputs = $('input:text[id^="pics"]');
  var values = $.map(inputs, function(n, i) {
      return n.value;
  });

  values = unique(values);

  return values.length === inputs.length;
}

function unique(arr) {
  var r = [];
  o:for(var i = 0, n = arr.length; i < n; i++) {
    for(var x = i + 1 ; x < n; x++) {
       if(arr[x]==arr[i]) continue o;
    }
    r[r.length] = arr[i];
  }
  return r;
}
Russ Cam
thanks. I was trying out your code but var inputs returns false in the following debug of your code. var inputs = $('input:text[id^="pics"]'); alert('here1 : ' + inputs);
nevermind. I was missing name attribute
@josh - take a look here http://jsbin.com/ivigo . add /edit to the url if you want to see the code
Russ Cam
One thing to bear in mind - At the moment this solution does not require a value in each element. If one value is an empty string, it will still validate to true. Is this acceptable?
Russ Cam
yes. that is fine. thanks! your solution def. works on jsbin. but for some odd reason if i take same code as jsbin and put iton the server (where I am testing) it does not work. Guess I need to figure that outmyesef. thanks again
please take a look once why it is not working. everything looks alright to me! unless some server setting stops jquery?? http://lorigrahamdesign.com.previewdns.com/admin/test1.html
you're also using mootools on the site which also uses the $ shorthand to define its selector function. wrap the script in (function($) { ... code above here ... })(jQuery); and all will be good. You could look at jQuery's $.noConflict() as an alternate solution - http://docs.jquery.com/Core/jQuery.noConflict
Russ Cam
A: 
<form onsubmit="return validate()" >
<input type="text" id="pics1" value="test1" />
<input type="text" id="pics2" value="test2" />
<input type="text" id="pics3" value="test3" />
<input type="submit" />
<br/><label id="error" />
</form>
<script>
function validate(){
    var inputs=document.getElementsByTagName("input");
    var pics1_temp,pics3_temp;
    for (var i=0;i<inputs.length;i++){
     if(inputs[i].id.indexOf("pics")>-1)
     {
      if(inputs[i].id=="pics1")
       pics1_temp=inputs[i].value;
      else if(inputs[i].id=="pics3")
       pics3_temp=inputs[i].value;
     }
    }

    if(pics1_temp!=pics3_temp)
    {
     document.getElementById("error").innerHTML="Pic1 must match with pic3";
     return false;
    }
    else return true;
}
</script>
Xinus