Computing.Net > Forums > Programming > client side validation - javascript

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

client side validation - javascript

Reply to Message Icon

Name: Terry
Date: December 8, 2003 at 13:25:51 Pacific
OS: XP
CPU/Ram: 1.8/ 512ddr
Comment:

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>



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: December 8, 2003 at 13:55:52 Pacific
Reply:

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


0

Response Number 2
Name: Terry
Date: December 8, 2003 at 14:41:05 Pacific
Reply:

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.


0

Response Number 3
Name: Terry
Date: December 8, 2003 at 14:44:17 Pacific
Reply:

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>


0

Response Number 4
Name: SN
Date: December 8, 2003 at 18:09:48 Pacific
Reply:

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


0

Response Number 5
Name: Rashar
Date: December 9, 2003 at 05:19:18 Pacific
Reply:

Thanks for your help SN. I'll try that out.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: client side validation - javascript

Asp.net help! www.computing.net/answers/programming/aspnet-help/13485.html

Which tool/language should i use? www.computing.net/answers/programming/which-toollanguage-should-i-use/13639.html

Javascript www.computing.net/answers/programming/javascript/1469.html