By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Validating uniqueness of form inputs

P: n/a
Hi all.

I have a form with a few text boxes which need to be validated client-side.
The validation is: check that every single text box has a unique string value.
I.e., I need to check that there are no two textboxes that both contain,
for example, the string 'hello'.
Is there an efficient way to do this kind of validation in javascript?

Thank you very much for your help.

Anna
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Anna wrote:
I have a form with a few text boxes which need to be validated client-side.
The validation is: check that every single text box has a unique string value.


You could iterate through each input text whose value should be unique
and log the results into an object: when a property has already been
defined, this means you have a duplicate.
<form onsubmit="return v(this)">
<input type="text" name="n1_UNIQUE" title="player 1"><br>
<input type="text" name="n2_UNIQUE" title="player 2"><br>
<input type="text" name="n3_UNIQUE" title="player 3"><br>
<input type="text" name="n4_UNIQUE" title="player 4"><br>
<input type="text" name="n5"><br>
<input type="submit">
</form>

<script type="text/javascript">
function v(frm){
var h={}, ii, el=frm.elements, val, err=[], index=1;
function p(s){err.push((index++)+" - "+s);}
function c(fld, bErr){fld.style.backgroundColor=bErr?"#c00":"";}

for(ii=0; ii<el.length; ii++){
if(el[ii].type && el[ii].type.toLowerCase()=="text"){
if(el[ii].name.indexOf("UNIQUE")!=-1){
//we're in a field whose value should be unique
val=el[ii].value.toLowerCase(); //case-insensitive
if(typeof h[val] != "undefined") {
// that's a duplicate, log an error
p("The field "+el[ii].title+" is a duplicate!");
c(el[ii], true);
}else{
// first time encoutered
h[val] = true;
c(el[ii], false);
}}}}

return err.length==0 || (alert(err.join("\n")), false);
}
</script>
HTH
Yep.
Jul 20 '05 #2

P: n/a
an**@ubaccess.com (Anna) writes:
I have a form with a few text boxes which need to be validated client-side.
The validation is: check that every single text box has a unique string value.
I.e., I need to check that there are no two textboxes that both contain,
for example, the string 'hello'.
Is there an efficient way to do this kind of validation in javascript?


You will have to run through the checkboxes and remember the strings
you see. Remembering is best done with an object, setting
obj[str]=true
when you first meet the string str, and checking for each string whether
obj[str]
is true.

---
var textBoxes = ['textboxName1','textboxName2',...,'textboxNameN'];
function checkBoxes(formRef) {
var cache = new Object();
for (var i=0;i<textBoxes.length;i++) {
var str = formRef.elements[textBoxes[i]].value;
if (cache[str]) { /* DUPLICATE FOUND */
/* DO SOMETHING */
return false;
}
cache[str]=true;
}
return true;
}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.