Connecting Tech Pros Worldwide Forums | Help | Site Map

How to best compare three strings

Newbie
 
Join Date: May 2007
Posts: 3
#1: May 19 '07
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

Newbie
 
Join Date: May 2007
Posts: 3
#2: May 20 '07

re: How to best compare three strings


Does anyone have any suggestions?

Thanks
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#3: May 20 '07

re: How to best compare three strings


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

Expand|Select|Wrap|Line Numbers
  1. var value1 = 'inputvalue1';
  2. var value2 = 'inputvalue2';
  3. var value3 = 'inputvalue3';
  4.  
  5. // if value1 matches value2 and value1 matches value3 then value2 must
  6. // match value3 ... so we needn't to compare them explicitly
  7.  
  8. if (value1 == value2 && value1 == value3) {
  9.     // all 3 values match
  10. } else {
  11.     // one of the values doesn't match
  12. }
  13.  
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 ...
Newbie
 
Join Date: May 2007
Posts: 3
#4: May 20 '07

re: How to best compare three strings


Gits... thanks for the hint. I'll give that a try.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#5: May 20 '07

re: How to best compare three strings


Quote:

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:

Expand|Select|Wrap|Line Numbers
  1. // you may fill a js-object 'onchange' of your textboxes
  2.  
  3. var inputs = {
  4. };
  5.  
  6. // this function fills the objects data - call it onchange with param 'this'
  7. // that passes the elements-ref itself to the function
  8.  
  9. function fill_inputs_object(obj) {
  10.     inputs[obj.id] = obj.value;    
  11. }
  12.  
  13. // check the form before submitting it
  14.  
  15. function check_form() {
  16.     var val = true;
  17.     var value1 = null;
  18.  
  19.     for (var i in inputs) {
  20.         var inp = inputs[i];
  21.  
  22.         // check for emptyness of first value
  23.         if (inp == '') {
  24.             // you may produce a error-message here
  25.             val = false;
  26.             // val is false, so we break the loop
  27.             break;
  28.         } else {
  29.             // value is not empty and we remember it for comparing it with
  30.             // the others
  31.             if (value1 == null) {
  32.                 value1 = inp;
  33.             } else {
  34.                 // we compare the inp with value1
  35.                 if (value1 != inp) {
  36.                     // you may produce a error-message here
  37.                     val = false;
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.     }
  43.  
  44.     return val;
  45. }
  46.  
thats an idea too ... we should beautify that solution ... :) until it looks a little bit ugly with much of those indentations ...

kind regards
Reply