Link to home
Start Free TrialLog in
Avatar of JohnHind
JohnHind

asked on

how to test if control exists on form

In vbscript, when the user submits a form, I want to check a value has been put into a control, but in some cases the control might not exist.

I want to do something like:

if IsNull(document.myForm.edtUsername.value) = true then....

but it will raise an error if the control does not exist. Any ideas ?

TIA
John
Avatar of jitganguly
jitganguly

Do it like this

function IsBlank(strFieldName,strMessage)
{
if(eval("document.admin." + strFieldName + ".value == ''"))
{
alert(strMessage);
eval("document.admin." + strFieldName + ".focus()");
return false;
}
}
<form method=post action="Admin.asp" name="admin" onsubmit="return validatePassword()">
<tr>
<td>Server</td>
<td><input type=text name="dbserver" value="<%=dbserver%>" <%=sRead%> onblur="return IsBlank('dbserver','Server Name Cannot be blanks');"></td>
</tr>
<tr>
<td>Database</td>
<td><input type=text name="dbdatabase" value="<%=dbdatabase%>" <%=sRead%> onblur="return IsBlank('dbdatabase','Database Name Cannot be blanks');"></td>
</tr>
<tr>
<td>Database User Id</td>
<td><input type=text name="dbuser" value="<%=dbuser%>" <%=sRead%> onblur="return IsBlank('dbuser','Database User ID Cannot be blanks');"></td>

For each columns/fields in html form I pass form field name and error message on onblur to a javascript function and that function does the validations

You can even do it on form onsubmit. I have a separate function on onsubmit of this thats why I do it on onblur
ASKER CERTIFIED SOLUTION
Avatar of jitganguly
jitganguly

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnHind

ASKER

jitganguly

I need to detect if the actual control itself exists, not if it has a value.

thanks
John
you didn't answer the question exactly, but you were the only person to answer, so you get the points !

John