You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2002/08/07 07:51:13 UTC
DO NOT REPLY [Bug 11520] New: -
[validator-rules.xml] javascript validateRequired() function does not work with radio objects
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11520>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND
INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=11520
[validator-rules.xml] javascript validateRequired() function does not work with radio objects
Summary: [validator-rules.xml] javascript validateRequired()
function does not work with radio objects
Product: Struts
Version: 1.1 Beta 1
Platform: PC
OS/Version: Windows NT/2K
Status: NEW
Severity: Normal
Priority: Other
Component: Validator Framework
AssignedTo: struts-dev@jakarta.apache.org
ReportedBy: pierre.delisle@sun.com
The javascript validateRequired() function in validator-rules.xml does not
work with radio objects.
I'm no JavaScript expert, but this seems to come from the fact that
form[oRequired[x][0]].type is undefined for a radio object (at least
using NN 4.76 and IE 6). The radio object yields an array, and each value
of the array must be validated to see if at least one of them has its
checked property set to true.
A sample page is included below, with validateRequired2() properly validating
the radio fields. The code is quite ugly, but could not find a way in javascript
to figure out how to test if an object is an array or not.
----------------------
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
function displayTypes() {
alert(
"textField type is: " + document.form.textField.type + "\n" +
"textField type is: " + document.form["textField"].type + "\n" +
"passwordField type is: " + document.form.passwordField.type + "\n" +
"selectField type is: " + document.form.selectField.type + "\n" +
"radioField type is: " + document.form.radioField.type + "\n" +
"radioField type is: " + document.form["radioField"].type + "\n" +
"radioField[0] type is: " + document.form.radioField[0].type);
}
function displayValues() {
alert(
"textField value is: " + document.form.textField.value + "\n" +
"passwordField value is: " + document.form.passwordField.value + "\n" +
"selectField value is: " + document.form.selectField.value + "\n" +
"selectField selectedIndex is: " +
document.form.selectField.selectedIndex + "\n" +
"radioField value is: " + document.form.radioField.value + "\n" +
"radioField[0] value is: " + document.form.radioField[0].value + "\n" +
"radioField[0] checked is: " + document.form.radioField[0].checked);
}
function required () {
this.aa = new Array("textField", "textField is required.", new Function
("varName", " return this[varName];"));
this.ab = new Array("radioField", "radioField is required.", new Function
("varName", " return this[varName];"));
}
function validateRequired(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oRequired = new required();
for (x in oRequired) {
if ((form[oRequired[x][0]].type == 'text' ||
form[oRequired[x][0]].type == 'textarea' ||
form[oRequired[x][0]].type == 'select-one' ||
form[oRequired[x][0]].type == 'radio' ||
form[oRequired[x][0]].type == 'password') &&
(form[oRequired[x][0]].value == '')) {
if (i == 0) {
focusField = form[oRequired[x][0]];
}
fields[i++] = oRequired[x][1];
bValid = false;
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return bValid;
}
function validateRequired2(form) {
var bValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oRequired = new required();
for (x in oRequired) {
alert("object is: " + form[oRequired[x][0]].name);
if (form[oRequired[x][0]].length == 0) {
alert("0 length");
} else {
alert("not zero length: " +
form[oRequired[x][0]].length);
}
if ((form[oRequired[x][0]].type == 'text' ||
form[oRequired[x][0]].type == 'textarea' ||
form[oRequired[x][0]].type == 'select-one' ||
form[oRequired[x][0]].type == 'radio' ||
form[oRequired[x][0]].type == 'password')) {
alert("in yes");
if (form[oRequired[x][0]].value == '') {
if (i == 0) {
focusField = form[oRequired[x][0]];
}
fields[i++] = oRequired[x][1];
bValid = false;
}
} else if (form[oRequired[x][0]][0].type == 'radio') {
alert("is radio");
var radioChecked = false;
for (var j=0; j<form[oRequired[x][0]].length; j++) {
if (form[oRequired[x][0]][j].checked) {
radioChecked = true;
break;
}
}
if (!radioChecked) {
fields[i++] = oRequired[x][1];
bValid = false;
}
} else {
alert("in else");
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return bValid;
}
-->
</SCRIPT>
<form name="form">
<input type="text" name="textField" size="40" value=""></td>
<p>
<input type="password" name="passwordField" size="20" value=""></td>
<p>
<select name="selectField">
<option value="ONE">ONE</option>
<option value="TWO">TWO</option>
<option value="THREE">THREE</option>
<option value="FOUR">FOUR</option>
<option value="FIVE">FIVE</option>
</select>
<p>
<input type="radio" name="radioField" value="1">One<br>
<input type="radio" name="radioField" value="2">Two<br>
<input type="radio" name="radioField" value="3">Three<br>
<p>
<input type="button" value="Display Types" onclick="displayTypes()"/>
<input type="button" value="Display Values" onclick="displayValues()"/>
<input type="button" value="Validate OLD"
onclick="validateRequired(document.form)"/>
<input type="button" value="Validate NEW"
onclick="validateRequired2(document.form)"/>
</form>
</html>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>