Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one validate 15 text boxes for unique value.

Status
Not open for further replies.

uncleroydee

Technical User
Nov 28, 2000
79
US
I have a form that contains, among other things, 15 text boxes; the boxes are named NSN1 through NSN15 and are already checked to ensure that only numeric data is entered. I need to write a script to check that no value is duplicated in any of the 15 text boxes.

Thanks for any suggestions.
 
Sounds like you should load the values of the text boxes into an array and then loop through the array to check for duplicates. Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi - try this. It basically compares all values when you click a button - you'd attatch this script to the submit button I guess. Incidentally this version of the script only shows one 'duplicate error' and then dies without posting further error messages - easily changeable...

<html>
<head>
<title>Check textboxes</title>
<script language=&quot;JavaScript&quot;>
<!--
function checkValues(){
error=0;
allValues = new Array()
for (i=0;i<15;i++){
allValues = document.myform.elements.value
}
for (i=0;i<15;i++){
firstvalue = document.myform.elements.value
for (j=0;j<15;j++){
secondvalue = document.myform.elements[j].value
if (i != j) {
if (firstvalue == secondvalue) {
if (!error) {alert (&quot;Box &quot; + (i+1) + &quot; is equal to box &quot; + (j+1) + &quot; please amend!&quot;)}
error=true;
} // close if firstvalue = second value
} // close if i != j
} //close j
} // close i
if (!error) {alert(&quot;Form Is OK&quot;)}
}
//-->
</script>
</head>
<body>
<form name=&quot;myform&quot;>
<script language=&quot;JavaScript&quot;>
<!--
for (i=0;i<15;i++) {
document.write (&quot;Box &quot; + (i+1) + &quot;<input type='text' name='NSN&quot; + (i+1) + &quot;' value='' size='5'><br>&quot;)
}
//-->
</script>
<input type=&quot;button&quot; value=&quot;check&quot; onClick=&quot;checkValues()&quot;>
</form>
</body>
</html>

 
Hi

u would i think use two arrays to double check each other

something like this

array(14)

array(0) = NSN1
array(1) = NSN2
array(2) = NSN3
array(3) = NSN4
array(4) = NSN5
array(5) = NSN6
.
.
array(14) = NSN15

array2(14) = array(14)
error = 0

for i = 0 to 14 do
for j = 0 to 14 do
if(i <> j) then
if (array(i) = array(j)) then
error = 1
break;
end if
end if
next j
next i

if (error = 1) then
Response.Write(&quot;Duplication detected.&quot;)
end if

this is pseudo code, suppose to show you how the structure is done...and the algorithmn...i know this way is no the most efficent...if you want a more efficent way you should look at sorts and other searches...the one i am using is a sequence search and compare...binary search have better run time...by divide it into halfs...but the array have to be sorted...therefore you should either sort it before assign it or code a sorting algorithm like quicksort...

Thanks,
Hui Emagine Solutions, Inc.
 
I think Matthew P's code is going to work for me, with a couple of modifications.

First, I'd like to change the onSubmit handler to onBlur handler in every NSN textbox so the user get's an alert immediately on entering a duplicate NSN and moving out of the field. I don't see any problem here.

Second, my form already contains the 15 NSN fields within a table so the code &quot;<script language=&quot;JavaScript&quot;>
<!--
for (i=0;i<15;i++) {
document.write (&quot;Box &quot; + (i+1) + &quot;<input type='text' name='NSN&quot; + (i+1) + &quot;' value='' size='5'><br>&quot;)
}
//-->
</script>&quot;

after the form tag is throwing me off. X-)

What are your suggestions Matthew?
And, thanks for the code.

Roy
 
Here's how I've tried to adapt (butcher?) Matthew P's code.

Code:
   function checkValues(){
    error=0;
    allValues = new Array()
    for (i=0;i<15;i++){
    allValues = document.InsrtNSN.NSN(i+1).value
    }
    for (i=0;i<15;i++){
    firstvalue = document.InsrtNSN.NSN(i+1).value
    for (j=0;j<15;j++){    
    secondvalue = document.InsrtNSN.NSN(i+1).value[j]
    if (i != j) {
        if (firstvalue == secondvalue) {
    if (!error) {alert (&quot;Duplicate NSNs are not permitted.&quot;)}
    error=true;
    } // close if firstvalue = second value
    } // close if i != j
    } //close j
    } // close i
    }

Please note the value statements in the Javascript. I've also ommitted Matthew's script after the form tag (see his reply above)because the NSN fields already exist.

Lastly, here's the onBlur tag.
Code:
onBlur=&quot;(validate(this) && checkValues())&quot;

I'm not receiving any error messages when the page loads or when I try to insert duplicate NSNs, however the
Code:
checkValues()
function doesn't fire.

Thanks for the help, and the forbearance of my inexperience.
 
Here's another version firing through onBlur's.

For some reason some of my code got parsed out between open and close square brackets when I cut and pasted this in before - guess the program on the server thought it was TGML code or something :( if the same thing happens with this code (ie - it doesn't work) try getting it from my site at
Matt.

<html>
<head>
<title>Check textboxes</title>
<script language=&quot;JavaScript&quot;>
<!--
function checkValues(){
error=0;
allValues = new Array()
for (i=0;i<15;i++){
allValues = document.myform.elements.value
}
for (i=0;i<15;i++){
firstvalue = document.myform.elements.value
if (firstvalue != &quot;&quot;){
for (j=0;j<15;j++){
secondvalue = document.myform.elements[j].value
if (i != j) {
if (firstvalue == secondvalue) {
if (!error) {alert (&quot;You have already entered this value in box &quot; + (i+1) + &quot; !&quot;)}
error=true;
} // close if firstvalue = second value
} // close if i != j
} //close j
}
} // close i
if (!error) {}
}
//-->
</script>
</head>
<body>
<form name=&quot;myform&quot;>
<script language=&quot;JavaScript&quot;>
<!--
for (i=0;i<15;i++) {
document.write (&quot;Box &quot; + (i+1) + &quot;<input type='text' name='NSN&quot; + (i+1) + &quot;' value='' size='5' onBlur='checkValues()'><br>&quot;)
}
//-->
</script>
<input type=&quot;button&quot; value=&quot;check&quot; onClick=&quot;checkValues()&quot;>
</form>
</body>
</html>
 
Yes, I've just looked and the Tek-Tips server is parsing out some of my code!!

Basically there should be an i in square brackets between elements and .value on lines 10 and 13 of the code. Do a view source from the link to my site to get the correct version!!

Matt.
 
I got the code from your site, Matt, thanks. My dilemna now is that the fields NSN 1 through NSN15 are part of an existing table in my form, but your code creates the textboxes.

Would it work if I renamed the NSN boxes in my form to 'NSN&quot; + (i+1) =&quot;' as you have written in your code?

Roy
 
If you put TGML code tags around your code it should not bother it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
IF i understand correctly what's going on here you should just be able to add &quot;onBlur=checkValues();&quot; to your existing NSN 1 thru to NSN15. Then delete the code that creates the boxes. Klae

You're only as good as your last answer!
 
Klae & Matt,

Thanks for all of the help thus far. Here's where I am currently:

The code
Code:
    function checkValues(){
    error=0;
    allValues = new Array()
    for (i=0;i<15;i++){
    allValues = document.InsrtNSN.NSN1.value
    }
    for (i=0;i<15;i++){
    firstvalue = document.InsrtNSN.NSN1.value
    for (j=0;j<15;j++){    
    secondvalue = document.InsrtNSN.NSN2.value
    if (i != j) {
        if (firstvalue == secondvalue) {
    if (!error) {alert (&quot;Duplicate NSNs are not permitted.&quot;)}
    error=true;
    } // close if firstvalue = second value
    } // close if i != j
    } //close j
    } // close i
    }
(Note the value names NSN1 and NSN2 instead of NSN
Code:
[i] & [j]
.)
works for the first two textboxes with an onClick event handler in the Submit button object, but still does not work with the onBlur handlers (I am trying to fire two functions with the onBlur handler.), but I can live with that.

While
Code:
    function checkValues(){
    error=0;
    allValues = new Array()
    for (i=0;i<15;i++){
    allValues = document.InsrtNSN.NSN[i].value
    }
    for (i=0;i<15;i++){
    firstvalue = document.InsrtNSN.NSN[i].value
    for (j=0;j<15;j++){    
    secondvalue = document.InsrtNSN.NSN[j].value
    if (i != j) {
        if (firstvalue == secondvalue) {
    if (!error) {alert (&quot;Duplicate NSNs are not permitted.&quot;)}
    error=true;
    } // close if firstvalue = second value
    } // close if i != j
    } //close j
    } // close i
    }
still does not work with either onClick or onBlur event handlers with my textboxes named &quot;NSN1&quot; through &quot;NSN15&quot;. I get the error message &quot;document.InsrtNSN.NSN is not an object&quot;.

I feel like you guys have helped me get this real close, it's just a matter of getting the function to recognize the list of NSN textbox names.

Since I only have 15 NSN textboxes to validate with this function, could I just hard code the names somehow?

Thanks again,

Roy
 
Ok,

See if this helps..

replace your for loops with the format of...

for (i=1; i<=15; i++)

also change i for j when using your j for loop.

Also, replace &quot;allvalues =&quot; line with this...

allValues =
eval(&quot;document.InsrtNSN.NSN&quot; + i + &quot;.value&quot;);

then change your &quot;firstvalue =&quot; and &quot;secondvalue =&quot; lines to have the same eval() functions in them in the same format. This should get the correct names of the textboxes in code.

Your code should look like this...

function checkValues(){
error=0;
allValues = new Array()
for (i=1;i<=15;i++){
allValues =
eval(&quot;document.InsrtNSN.NSN&quot; + i + &quot;.value&quot;);
}
for (i=1;i<=15;i++){
firstvalue =
eval(&quot;document.InsrtNSN.NSN&quot; + i + &quot;.value&quot;);}
for (j=1;j<=15;j++){
secondvalue =
eval(&quot;document.InsrtNSN.NSN&quot; + j + &quot;.value&quot;)
if (i != j) {
if (firstvalue == secondvalue) {
if (!error) {alert (&quot;Duplicate NSNs are not permitted.&quot;)}
error=true;
} // close if firstvalue = second value
} // close if i != j
} //close j
} // close i
}

Klae

You're only as good as your last answer!
 
Klae,

Happy Monday to you.

First, as always, thanks for your post.

Second, a question about the name of the NSN fields within my form. The NSN input fileds are currently written as such:
Code:
<td><Input type = text size = &quot;13&quot; id=NSN1 name=NSN1 maxlength=&quot;13&quot; onBlur=&quot;validate(this)&quot;></td>
. (&quot;Validate(this)&quot; is another function, unrelated to this issue) Should the name, &quot;NSN1&quot;, be written differently, like &quot;NSN(i)&quot; or something?

Third, it appears that once a user attempts to submit duplicate NSNs the alert continues to fire even after the user corrects the mistake.

Fourth, a new wrinkle; I just realized that the user does not have to complete all 15 rows in order to submit the form. Do I need to adapt the script to allow duplicate empty NSN fields (null values) yet not allow duplicate positive values? If so, do you have any suggestions?

Thanks again for your help, I hope you find this a worthy challenge and not a royal pain.

Roy

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top