How to best compare three strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ultragc
    New Member
    • May 2007
    • 3

    How to best compare three strings

    Hi all. I am starting on javascript and need some help. Basically, I have a form in which I need to compare three text fields (string1, string2, and string3). The user must enter the exact text on all three strings before they are allow to submit it.

    In short, the user must type in their initials in three fields to acknowledge that the are agreeing to the terms. Before submitting, the fields must be validated for exact match.

    Any suggestions would be appreciated.

    Thanks.
    Ultra
  • ultragc
    New Member
    • May 2007
    • 3

    #2
    Does anyone have any suggestions?

    Thanks

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      hi ... you have to get the values out of your textboxes and simply compare them ... you may compare them like this:

      [CODE=javascript]
      var value1 = 'inputvalue1';
      var value2 = 'inputvalue2';
      var value3 = 'inputvalue3';

      // if value1 matches value2 and value1 matches value3 then value2 must
      // match value3 ... so we needn't to compare them explicitly

      if (value1 == value2 && value1 == value3) {
      // all 3 values match
      } else {
      // one of the values doesn't match
      }
      [/CODE]

      note ... think you should check before this whether the value1 == '' or not ... in that case the user didn't enter a value and it seems that you want him to do so ...

      may be you want to check for some length of the input or characters too ... do that before comparing the values 1-3 ... you always only need to check value1 for that ... last step compares value2 and value3 to the validated value1 only ...

      hope this helps ...

      Comment

      • ultragc
        New Member
        • May 2007
        • 3

        #4
        Gits... thanks for the hint. I'll give that a try.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          Originally posted by ultragc
          Gits... thanks for the hint. I'll give that a try.
          ... please show your solution at least ... or if you encounter problems post the questions :) ... it should work with such a compare-function ... but somtimes there are better ways to do such things ... the shown is a simple one have a look at the following too:

          [CODE=javascript]
          // you may fill a js-object 'onchange' of your textboxes

          var inputs = {
          };

          // this function fills the objects data - call it onchange with param 'this'
          // that passes the elements-ref itself to the function

          function fill_inputs_obj ect(obj) {
          inputs[obj.id] = obj.value;
          }

          // check the form before submitting it

          function check_form() {
          var val = true;
          var value1 = null;

          for (var i in inputs) {
          var inp = inputs[i];

          // check for emptyness of first value
          if (inp == '') {
          // you may produce a error-message here
          val = false;
          // val is false, so we break the loop
          break;
          } else {
          // value is not empty and we remember it for comparing it with
          // the others
          if (value1 == null) {
          value1 = inp;
          } else {
          // we compare the inp with value1
          if (value1 != inp) {
          // you may produce a error-message here
          val = false;
          break;
          }
          }
          }
          }

          return val;
          }
          [/CODE]

          thats an idea too ... we should beautify that solution ... :) until it looks a little bit ugly with much of those indentations ...

          kind regards

          Comment

          Working...