...

View Full Version : Text Box Validation



philiprakusen
12-17-2004, 01:39 PM
Anyone know of a bit of JavaScript that will alert if the user has entered a value in more that one text box from a set of three.

One text box must have a value, but only one.

So far I have the following to check that they are not all empty:

function validateForm() {
if (document.upload.discount_price.value == '' &&
document.upload.discount_percent.value == '' &&
document.upload.discount_value.value == '') {
alert ('Please enter a value for either the discount price, percentage, or value.');
return false;
}
else if(

...


Any suggestions would be appreciated.

Thanks.

philiprakusen
12-17-2004, 01:56 PM
...unless anyone can think of a better version than this:

function validateForm() {
if (document.upload.discount_price.value == '' &&
document.upload.discount_percent.value == '' &&
document.upload.discount_value.value == '') {
alert ('Please enter either the discount price, percentage, or value.');
return false;
}
else if (document.upload.discount_price.value != '' &&
document.upload.discount_percent.value != '' &&
document.upload.discount_value.value != '') {
alert ('Please enter only one type of discount.');
return false;
}
else if (document.upload.discount_price.value != '' &&
document.upload.discount_percent.value != '') {
alert ('Please enter only one type of discount.');
return false;
}
else if (document.upload.discount_percent.value != '' &&
document.upload.discount_value.value != '') {
alert ('Please enter only one type of discount.');
return false;
}
else if (document.upload.discount_price.value != '' &&
document.upload.discount_value.value != '') {
alert ('Please enter only one type of discount.');
return false;
}
}

requestcode
12-17-2004, 02:00 PM
How about this:


function validateForm() {
if (document.upload.discount_price.value == '' &&
document.upload.discount_percent.value == '' &&
document.upload.discount_value.value == '') {
alert ('Please enter a value for either the discount price, percentage, or value.');
return false;
}
else
{
count=0
if(document.upload.discount_price.value.length > 0)
{count++}
if(document.upload.discount_percent.value.length > 0)
{count++}
if(document.upload.discount_value.value.length > 0)
{count++}
if(count>1)
{alert ("You may only fill in one.")}
}
}

philiprakusen
12-17-2004, 02:09 PM
That is perfect... Thanks.

Willy Duitt
12-17-2004, 02:29 PM
A little late but:



<script type="text/javascript">
<!--//
function validate(form){
var hasValue = [], message = '';
for(var i=0;i<form.elements.length;i++){
var input = form.elements[i];
if(input.className.match(/onlyOne/gi) && input.value.length > 0){
message += '\n'+input.name.replace('_',' ').toUpperCase();
hasValue.push(input.name);
}
}

if(hasValue.length == 0){
alert ('Please enter a value for either the:\ndiscount price, percentage, or value.');
return false;
}

if(hasValue.length > 1){
for(var i=0;i<hasValue.length;i++){
form[hasValue[i]].value = '';
} alert('You have chosen:\n'+message+'\n\nPlease chose only one!!');
return false;
} return true;
}
//-->
</script>
</head>

<body>
<form onsubmit="return validate(this)">
<input class="onlyOne" name="discount_price"><br>
<input class="onlyOne" name="discount_percent"><br>
<input class="onlyOne" name="discount_value"><br>
<input type="submit" value="Test">
</form>


.....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum