View Full Version : Javascript Help
clake
03-22-2001, 04:14 PM
I'm trying to set up some error trapping that #1 makes sure one of 2 radio buttons is selected and, #2 once that selection has been made, makes sure that a corresponding text box is filled in before submitting (each radio button has a corresponding text box). I can't quite get this to work. Here's the code:
if (form.fldLoanType[0].checked) {
if (!CheckText("Duration", form.fldSTDuration.value, true)) {
form.fldSTDuration.focus();
return false;
}
}
if (form.fldLoanType[1].checked) {
if (!CheckText("Duration", form.fldLTDuration.value, true)) {
form.fldLTDuration.focus();
return false;
}
}
kdjoergensen
03-22-2001, 04:44 PM
What does your checkText() function look like ?
clake
03-22-2001, 04:54 PM
function CheckText(name, chkText, required) {
if (required && chkText == "") {
alert(name + " is required.");
return false;
}
var validChars = new Array("$",">","%","#","!","'","<","&","*","~",":","`","?","}","{","]","[","^","|","=","+","_",";",'"');
var indx = 0;
var str = "";
var isValid = true;
for (var i = 0; i < validChars.length; i++) {
str = validChars[i];
if (chkText.indexOf(str) >= 0) {
isValid = false;
i = validChars.length;
}
}
if (!isValid) {
alert("Only alpha numeric characters are allowed in " + name);
return false;
}
return true;
}
kdjoergensen
03-22-2001, 05:28 PM
Your isValid variable is only checking the last entry in the Array the way the script is constructed. I suggest
function CheckText(name, chkText, required) {
if (required && chkText == "") {
alert(name + " is required.");
return false;
}
var validChars = new Array("$",">","%","#","!","'","<","&","*","~",":","`","?","}","{","]","[","^","|","=","+","_",";",'"');
var indx = 0;
var str = "";
var isValid = true;
for (var i = 0; i < validChars.length; i++) {
str = validChars;
if (chkText.indexOf(str) >= 0) {
alert("Only alpha numeric characters are allowed in " + name);
return false;
}
}
return true;
}
clake
03-22-2001, 05:38 PM
No change. I've put an alert after this line: if (form.fldLoanType[0].checked) and it doesn't run it. I'm suspecting that it has to do with this line, but can't figure out why . . .
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.