Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Form Validation - must be unique entries into input[type="text"]
Old 03-16-2009, 12:31 PM Form Validation - must be unique entries into input[type="text"]
tuestunim's Avatar
Experienced Talker

Posts: 41
Name: Scott Brown
Location: Huddersfield, UK
Trades: 0
Hello,

I have a form. Three numbers will be entered into it. I don't want this form to be valid if any two or three of the three numbers are the same as each other (i.e. they all need to be different).

Any ideas? Hopefully this is an easy one for anyone who knows their JavaScript stuff. I'm running jQuery on this page as well so if that makes it easier, go for it.

HTML code as it stands:

Code:
<label for="firstchoice">1st Choice</label>
<input type="text" id="firstchoice" />
<label for="secondchoice">2nd Choice</label>
<input type="text" id="secondchoice" />
<label for="thirdchoice">3rd Choice</label>
<input type="text" id="thirdchoice" />
Thanks in advance

Scott.
tuestunim is offline
Reply With Quote
View Public Profile Visit tuestunim's homepage!
 
 
Register now for full access!
Old 03-16-2009, 02:06 PM Re: Form Validation - must be unique entries into input[type="text"]
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
HTML Code:
<script type="text/javascript">
var submitForm = true;
var firstChoice = document.getElementById("firstchoice");
var secondChoice = document.getElementById("secondchoice");
var thirdChoice = document.getElementById("thirdchoice");

if (firstChoice == secondChoice || secondChoice == thirdChoice) {
     submitForm = false;
} else if (firstChoice == thirdChoice) {
     submitForm = false;
}
</script>
I'm still learning JS, so this form is not perfect. Also your form needs the <form action="" method=""> ... </form> tags.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
Reply With Quote
View Public Profile Visit Decaf's homepage!
 
Old 03-17-2009, 10:16 AM Re: Form Validation - must be unique entries into input[type="text"]
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
this;
Code:
var firstChoice = document.getElementById("firstchoice")
will return the object NOT it's value.

So if they are all input elements, they will all match regardless of what the user has entered
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-17-2009, 03:29 PM Re: Form Validation - must be unique entries into input[type="text"]
Decaf's Avatar
Ultra Talker

Posts: 489
Name: Adam
Trades: 0
Quote:
Originally Posted by chrishirst View Post
this;
Code:
var firstChoice = document.getElementById("firstchoice")
will return the object NOT it's value.

So if they are all input elements, they will all match regardless of what the user has entered
Then would this work?

HTML Code:
<script type="text/javascript">
var name = name.value;
var email = email.value;

if (name == email) {
     alert("Error, your name can not match your email.");
}
</script>
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Decaf is offline
Reply With Quote
View Public Profile Visit Decaf's homepage!
 
Old 03-17-2009, 03:46 PM Re: Form Validation - must be unique entries into input[type="text"]
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
not quite;

you have to qualify each object with it's parentage.
so this;
Code:
var name = name.value;
var email = email.value;
would be
Code:
var name = document.formname.name.value;
var email = document.formname.email.value;
Unlike many OOP languages, javascript has no with object qualifier, but you can assign the parent(s) to a variable and use that. So it can be shortened to
Code:
var parent = document.form;
var name = parent.name.value;
var email = parent.email.value;
And there are of course several other ways to reference an element.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-18-2009, 07:27 AM Re: Form Validation - must be unique entries into input[type="text"]
tuestunim's Avatar
Experienced Talker

Posts: 41
Name: Scott Brown
Location: Huddersfield, UK
Trades: 0
Sorted, thanks for your help guys. This is what I ended up with
Code:
<form id="theform" action="choicesub.asp" name="theform" method="post" onsubmit="return validate();">
      <fieldset>
          <h3>Choices</h3>
            <label for="firstchoice">1st Choice</label>
            <input type="text" id="firstchoice" value="" />
            <label for="secondchoice">2nd Choice</label>
            <input type="text" id="secondchoice" value="" />
            <label for="thirdchoice">3rd Choice</label>
            <input type="text" id="thirdchoice" value="" />
      </fieldset>
    </form>
Code:
function validate() {
  
  var choiceone = document.theform.firstchoice.value;
  var choicetwo = document.theform.secondchoice.value;
  var choicethree = document.theform.thirdchoice.value;

  if (choiceone == choicetwo) {
    alert("Error - Two or more of your choices are the same. Please reset your choices and try again.");
    return false;
  }

  else if (choiceone == choicethree) {
    alert("Error - Two or more of your choices are the same. Please reset your choices and try again.");
    return false;
  }

  else if (choicetwo == choicethree) {
    alert("Error - Two or more of your choices are the same. Please reset your choices and try again.");
    return false;
  }
  
  return true;

}
What's better than it working is that I nearly understand it!

Scott.
tuestunim is offline
Reply With Quote
View Public Profile Visit tuestunim's homepage!
 
Reply     « Reply to Form Validation - must be unique entries into input[type="text"]
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23584 seconds with 12 queries