473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Validating uniqueness of form inputs

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
2 2205
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bildad | last post by:
The following 'book example' of validating input seems to be incomplete. Since it is a beginner's book it may be intentional for simplicity. But I would like to know how to make this program work...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
6
by: J Huntley Palmer | last post by:
What is the most efficient way to validate an input to conform to your needs? I need to make sure an input is a contiguous string with only printable characters (english alphabet+numbers only)...
21
by: gurdz | last post by:
Does anyone know how to perform data validation in C? I have searched google for every possible result, and I either end up with data validation for C++ or nothing at all. I have also searched...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.