English 中文(简体)
Javascript : To check if the fields in a form are not identical
原标题:

I have the following problem.

The following is the form i have

<input type="text" name="email1" />
<input type="text" name="email2" />
<input type="text" name="email3" />
<input type="text" name="email4" />
<input type="text" name="email5" />

I want all the 5 Email ids to be different (unique) i.e, i dont want the user to be entering the same Email IDs in all the fields.

I need this to be done in Javscript

Can someone please help me with this

Thanks,
Kartik

问题回答
for (var i = 1; i < 6; i++) {
    for (var j = i+1; j < 6; j++) {
        if (document.getElementsByName("email"+i)[0].value ==
            document.getElementsByName("email"+j)[0].value) {
            alert("Fields "+i+" and "+j+" are equal!");
        }
    }
}

You may want to add another check, if you allow several of the fields to stay empty.

In Javascript an Array can act as a stack (push/pop) in which you can push new items after checking they re not already in the stack.

var emailAddrs = [];
var item;

    for(var i = 0; i < 5; i++){
      item = document.getElementsByName("email"+i)[0].value;
      if(! emailAddrs.contains(item))
        emailAddrs.push(item);
    }

UPDATE

Once you have your array build, you can then use that as a basis to do other operations. Using the length property of the array, you can check how many items are in the array vs how many you expected to see.

You could also push identical items into another array and use this information to let the user know what needs changing

UPDATE 2

I just realised that the contains method I use is created by adding the following code:

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
}

This then creates a contains method to each array object.

If you re using jQuery, you can also do $.inArray(value, array)





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签