PDA

View Full Version : more form validation woes


hyefive
10-30-2002, 07:51 AM
Hello,

I'm having trouble with the following CheckForms function. The alert works fine if the first two fields are empty but I don't get it if fname.value, lname.value, or anything thereafter is empty upon submission.

Yet if I take out the first three if statements (validating listserv and OFFICE_COD or AGT_CODE) , I do get it for fname.value but not for lname.value or anything after.

Very strange to me... anyways, here it is:

<script>
function CheckForms(f){

if (f.listserv && f.listserv.selectedIndex == 0){
alert("Please select an Association")
return false;
}
if ((f.listserv && f.COD_TYPE.value == "AGENT" && (f.AGT_CODE.value == "" || f.AGT_CODE.value == null || isblank(f.AGT_CODE)))){
alert("Please enter an Agent Code")
return false;
}
if ((f.listserv && f.COD_TYPE.value == "OFFICE" && (f.OFFICE_COD.value == "" || f.OFFICE_COD.value == null || isblank(f.OFFICE_COD)))){
alert("Please enter an Office Code")
return false;
}
if (f.fname.value == "" || f.fname.value == null || isblank(f.fname)){
alert("Please enter first name")
return false;
}
if (f.lname.value == "" || f.lname.value == null || isblank(f.lname)){
alert("Please enter last name")
return false;
}
if (f.aphone.value == "" || f.aphone.value == null || isblank(f.aphone)){
alert("Please enter complete phone number")
return false;
}
if (f.userid.value == "" || f.userid.value == null || isblank(f.userid)){
alert("Please enter a user id")
return false;
}
if (f.pwd.value == "" || f.pwd.value == null || isblank(f.pwd)){
alert("Please enter a password")
return false;
}
}
</script>

<form action="step2.asp" method="post" onSubmit="return CheckForms(this)">
<table>
<tr>
<td>ASSOCIATION</td>
<% If Request.QueryString("COD_TYPE") = "AGENT" Then %>
<input type="hidden" name="OFFICE_COD" value="">
<td>AGENT CODE</td>
<% Else %>
<input type="hidden" name="AGENT_COD" value="">
<td>OFFICE CODE</td>
<% End If %>
</tr>

<tr>
<td><select name="listserv" size="1">
<option></option>
<option value="BEAR">BEAR</option>
<option value="EBRD">EBRD</option>
</select></td>
<% If Request.QueryString("COD_TYPE") = "AGENT" Then %>
<td><input name="AGT_CODE"></td>
<% Else %>
<td><input name="OFFICE_COD"></td>
<% End If %>
</tr>

<tr>
<td>First Name:</td>
<td><input name="fname"></td>
</tr>

<tr>
<td>Last Name:</td>
<td><input name="lname"></td>
</tr>

<tr>
<td>Phone:</td>
<td><input name="aphone"></td>
</tr>

<tr>
<td>UserID:</td>
<td><input name="userid"></td>
</tr>

<tr>
<td>Choose a Password:</td>
<td><input name="pwd" type="PASSWORD"></td>
</tr>

<tr>
<td>Enter your Password again:</td>
<td><input name="pwd2" type="PASSWORD"></td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;<input type="SUBMIT" value="Continue" id="SUBMIT1" name="SUBMIT1"></td>
</tr>

</table>
</form>

Thank you for any assistance!!

glenngv
10-30-2002, 08:19 AM
<script>
function CheckForms(f){

if (f.listserv.selectedIndex == 0){
alert("Please select an Association")
return false;
}
if (f.AGT_CODE && f.AGT_CODE.value == ""){
alert("Please enter an Agent Code")
return false;
}
if (f.OFFICE_COD && f.OFFICE_COD.value == ""){
alert("Please enter an Office Code")
return false;
}
if (f.fname.value == ""){
alert("Please enter first name")
return false;
}
if (f.lname.value == ""){
alert("Please enter last name")
return false;
}
if (f.aphone.value == ""){
alert("Please enter complete phone number")
return false;
}
if (f.userid.value == ""){
alert("Please enter a user id")
return false;
}
if (f.pwd.value == ""){
alert("Please enter a password")
return false;
}
return true;
}
</script>

you only check for existence of elements that may or may not appear on the page. In your case, they are AGT_CODE and OFFICE_CODE

hyefive
10-30-2002, 05:22 PM
Actually, I DO need to check for the existence of listserv as well because there will be instances where COD_TYPE is neither AGENT or OFFICE, and so subsequently the form won't be gathering listserv and AGT_CODE or OFFICE_COD. In my first description of the page, I omitted an if-then that checks for it, so it should read as follows:

---
<form action="step2.asp" method="post" onSubmit="return CheckForms(this)">
<table>
<tr>
<% Request.QueryString("COD_TYPE") = "AGENT" OR Request.QueryString("COD_TYPE") = "OFFICE" Then %>
<td>ASSOCIATION</td>
<% If Request.QueryString("COD_TYPE") = "AGENT" Then %>
<input type="hidden" name="OFFICE_COD" value="">
<td>AGENT CODE</td>
<% Else %>
<input type="hidden" name="AGENT_COD" value="">
<td>OFFICE CODE</td>
<% End If %>
</tr>

<tr>
<td><select name="listserv" size="1">
<option></option>
<option value="BEAR">BEAR</option>
<option value="EBRD">EBRD</option>
</select></td>
<% If Request.QueryString("COD_TYPE") = "AGENT" Then %>
<td><input name="AGT_CODE"></td>
<% Else %>
<td><input name="OFFICE_COD"></td>
<% End If %>
</tr>
<% End If %>

---- the rest the same----


But even if I checked for existence of listserv when I didn't need to I don't see how that affects anything regarding the initial problem... ???

hyefive
10-31-2002, 11:02 PM
I found my solution...

After I removed the portion that checks each .value for "== null" and "isblank()", it works fine.


:)

glenngv
11-04-2002, 03:21 AM
if you looked at the code i posted, you should have found the solution at once. :)
You only had to change this line:

if (f.listserv.selectedIndex == 0){

to:

if (f.listserv && f.listserv.selectedIndex == 0){

Oakendin
09-14-2004, 07:31 PM
How do you check for the existence of a function. I know how to do it for a variable, but never tried for a function until.

I call a function on a page, if its not there, do nothing.

glenngv
09-16-2004, 03:10 AM
if (myFunc) { //function exists
myFunc(args);
}

or:

if (typeof myFunc == "function") { //function exists
myFunc(args);
}