String matching in javascript

Status
Not open for further replies.

Sridhar_Rao

In the zone
Guys, here is the situation. There are four elements in the form, let's say:
Code:
<input type="text" name="a1" id="a1">
<input type="text" name="a2" id="a2">
<input type="text" name="a3" id="a3">
<input type="text" name="a4" id="a4">

I want to ensure that data entered by the user is not identical in any of these elements. I mean no two elements can have same value. How can this be done in javascript with minimal coding?

Note: It is possible that one or more them may be left blank by the user.
 
Last edited:

n2casey

Super Hero - Super Powers
Below is simple solution for it.


Code:
    function CheckValues()
    {
        var avalues=new Array();
        var avalues[0]=document.getElementById('a1').value;
        var avalues[1]=document.getElementById('a2').value;
        var avalues[2]=document.getElementById('a3').value;
        var avalues[3]=document.getElementById('a4').value;
        
        for(i=0; i<avalues.length; i++)
        {
            for(j=0; j<avalues.length; j++)
                if(avalues[j] != '' && avalues[i] == avalues[j])
                    return false;
        }
        
        return true;
    }

Oops, I forgot the blank textbox part. Well now it's OK. :)
 
Last edited:
Status
Not open for further replies.
Top Bottom