Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Looking for some help on validating duplicate entries in the following code...i would like to prompt the user with an alert that a duplicate entry has been made...thanks in advance.
<script language = "javascript">
function validateChoice()
{
// Get the source element.
var choice = event.srcElement;
// Valid numbers.
var num = "0123456789";
event.returnValue = true;
/* Loop over Choices. If any Choice selected is not a number,
set the return value to false. */
for (var intLoop = 0; intLoop < choice.value.length; intLoop++)
{
for (var intLoop =0; intLoop < choice.value.length; intLoop++)
if (num.indexOf(choice.value.charAt(intLoop))) == (num.indexOf(choice.value.charAt(intLoop)))
alert ("Duplicate Entry"!)
event.returnValue=false;
}
if (-1 == num.indexOf(choice.value.charAt(intLoop)))
event.returnValue=false;
if (!event.returnValue) // Bad value
choice.className = "badValue"; // change colour of incorrect choice.
else
// Clear class to use default rendering.
choice.className="";
}
</SCRIPT>

Hmmmm...Some interesting code here. You'll have to give some information on what kind of data you are checking duplicates for in order for us to give too much information, but I'll make some comments:
if (num.indexOf(choice.value.charAt(intLoop))) == (num.indexOf(choice.value.charAt(intLoop)))
The above if statement will always execute...like saying if (x==x). You say in one place that you're checking to see if all letters of the input are numbers and another to see if there are duplicate entries (entries in what???), so I can't tell you what you want. I can only tell you that what you have is wrong.
if (-1 == num.indexOf(choice.value.charAt(intLoop)))
You defined intLoop in the previous block (the for loop)...you shouldn't use it outside the block in which it was defined. I don't know how strict javascript is about this sort of thing, but it's bad style, at least.
If you can give some more information or a link to the page you're using that would allow us to help you better.
-SN

To clearify: What i'm trying to validate is if the user enters the same number twice, then to alert the user that a duplicate entry has been made. The above code is validation for characters other than numbers, as well as negative numbers. Therefore the user must enter in a number >=1.

Sorry, refined code...
<STYLE TYPE="text/css">
.badValue {background:red; color:white}
</STYLE>
<script language = "javascript">
function validateChoice()
{
// Get the source element.
var choice = event.srcElement;
// Valid numbers.
var num = "0123456789";
event.returnValue = true;
/* Loop over Choices. If any Choice selected is not a number,
set the return value to false. */
if (-1 == num.indexOf(choice.value.charAt(intLoop)))event.returnValue=false;
if (!event.returnValue) // Bad value
choice.className = "badValue"; // change colour of incorrect choice.
else
// Clear class to use default rendering.
choice.className="";
}
</SCRIPT>

Okay, I think I get what you're asking now. You've taken out the loop now...I don't know why you did that, but we'll put it back in. I don't have time to explain why what you're doing is wrong, but I will give some hints as to how you can make your code shorter, faster, and easier to understand.
You have a certain number of characters that can be valid, right? 0-9 initially, then if you read in a 0, then only 1-9 are valid, and then if you read in a 5, then only 1-4 & 6-9 are valid, and so on. So why not just think in terms of valid characters and forget about worrying about whether they are invalid because they are alphabetical or because they have already been used. How, you ask?
Use your "num" string, and for each number in the input, check to see if it is in the "num" string (you already have that code), and if it is, remove that number from num. num.replace(character,"") removes a character from a string. Then go back and do it again. If you encounter a number twice, you'll catch the second one because it won't be in "num".
Good luck,
-SN

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |