Validating uniqueness of form inputs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anna

    #1

    Validating uniqueness of form inputs

    Hi all.

    I have a form with a few text boxes which need to be validated client-side.
    The validation is: check that every single text box has a unique string value.
    I.e., I need to check that there are no two textboxes that both contain,
    for example, the string 'hello'.
    Is there an efficient way to do this kind of validation in javascript?

    Thank you very much for your help.

    Anna
  • Yann-Erwan Perio

    #2
    Re: Validating uniqueness of form inputs

    Anna wrote:
    [color=blue]
    > I have a form with a few text boxes which need to be validated client-side.
    > The validation is: check that every single text box has a unique string value.[/color]

    You could iterate through each input text whose value should be unique
    and log the results into an object: when a property has already been
    defined, this means you have a duplicate.


    <form onsubmit="retur n v(this)">
    <input type="text" name="n1_UNIQUE " title="player 1"><br>
    <input type="text" name="n2_UNIQUE " title="player 2"><br>
    <input type="text" name="n3_UNIQUE " title="player 3"><br>
    <input type="text" name="n4_UNIQUE " title="player 4"><br>
    <input type="text" name="n5"><br>
    <input type="submit">
    </form>

    <script type="text/javascript">
    function v(frm){
    var h={}, ii, el=frm.elements , val, err=[], index=1;
    function p(s){err.push(( index++)+" - "+s);}
    function c(fld, bErr){fld.style .backgroundColo r=bErr?"#c00":" ";}

    for(ii=0; ii<el.length; ii++){
    if(el[ii].type && el[ii].type.toLowerCa se()=="text"){
    if(el[ii].name.indexOf(" UNIQUE")!=-1){
    //we're in a field whose value should be unique
    val=el[ii].value.toLowerC ase(); //case-insensitive
    if(typeof h[val] != "undefined" ) {
    // that's a duplicate, log an error
    p("The field "+el[ii].title+" is a duplicate!");
    c(el[ii], true);
    }else{
    // first time encoutered
    h[val] = true;
    c(el[ii], false);
    }}}}

    return err.length==0 || (alert(err.join ("\n")), false);
    }
    </script>


    HTH
    Yep.

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Validating uniqueness of form inputs

      anna@ubaccess.c om (Anna) writes:
      [color=blue]
      > I have a form with a few text boxes which need to be validated client-side.
      > The validation is: check that every single text box has a unique string value.
      > I.e., I need to check that there are no two textboxes that both contain,
      > for example, the string 'hello'.
      > Is there an efficient way to do this kind of validation in javascript?[/color]

      You will have to run through the checkboxes and remember the strings
      you see. Remembering is best done with an object, setting
      obj[str]=true
      when you first meet the string str, and checking for each string whether
      obj[str]
      is true.

      ---
      var textBoxes = ['textboxName1', 'textboxName2', ...,'textboxNam eN'];
      function checkBoxes(form Ref) {
      var cache = new Object();
      for (var i=0;i<textBoxes .length;i++) {
      var str = formRef.element s[textBoxes[i]].value;
      if (cache[str]) { /* DUPLICATE FOUND */
      /* DO SOMETHING */
      return false;
      }
      cache[str]=true;
      }
      return true;
      }
      ---

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...