473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
May 19 '07 #1
4 6227
Does anyone have any suggestions?

Thanks
May 19 '07 #2
gits
5,390 Expert Mod 4TB
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 ...
May 20 '07 #3
Gits... thanks for the hint. I'll give that a try.
May 20 '07 #4
gits
5,390 Expert Mod 4TB
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
May 20 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Grahammer | last post by:
Is it possible to compare two objects of the same class directly to each other? In the code below I'm comparing "Search" against "oneForm.thisForm"... Both of these objects are of the class...
3
by: Drew | last post by:
Hello - I am a converted VB programmer. What I am trying to do it compare two strings in an if statement. The problem is that when I use string.compare it always returns a negative 1. I have...
1
by: Rasika Wijayaratne | last post by:
Hello, Can I get feedback on these .NET coding best practices please. Thanks to Konrad Rudolph and Jon Skeet for the replies to my previous post on 'Writing Properties.' Thanks in advance to all...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.