Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to validate empty fields? 1

Status
Not open for further replies.

longmatch

Programmer
Nov 1, 2001
406
Dear All:
I would like to validate user input when "submit" button is clicked. I tried to use following syntax to test whether this field is empty or not. Unfortunately, the msgbox "sex can not be blank" always poped out even though this field is not blank? I do not know why?

Thanks


if document.ProcedureData.sex.value = "" then
msgbox "Sex can not be blank"
validation = false
else
validation = true
end if
 
assuming this is client side vbscript you should do it like this.

<html>
<head>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
Function Form1_OnSubmit()
Form1_OnSubmit = False
Set Form = Document.Form1

If (Form.UserName.Value) = &quot;&quot; Then
MsgBox &quot;Enter your name.&quot;&var1, vbCritical, &quot;Need Input&quot;
Form.UserName.Focus
Else
Form1_OnSubmit = True
End If
End Function
-->
</SCRIPT>
<BODY>
<form name=Form1 action=ReachOut.asp method=POST>
Name <INPUT type=TEXT name=&quot;UserName&quot;> (Required)<BR>
<INPUT type=SUBMIT value=&quot;Submit Comments&quot;>
</FORM>
</BODY>
</HTML> I help at your own risk, if I brake it sorry! But if I fixed it, let me know.[thumbsup2]
admin@onpntwebdesigns.com
 
Thank you for your help. I modified my code based your, the problems solved. But if there are several fields need to validate, what is the best way to do that?


Haijun
 
if you only want a few out of the form to be checked you can do it this way (elseif)
<html>
<head>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
Function Form1_OnSubmit()
Form1_OnSubmit = False
Set Form = Document.Form1

If (Form.UserName.Value) = &quot;&quot; Then
MsgBox &quot;Enter your name.&quot;&var1, vbCritical, &quot;Need Input&quot;
Form.UserName.Focus
elseif (Form.password.Value) = &quot;&quot; Then
MsgBox &quot;Enter your pass word please.&quot;&var1, vbCritical, &quot;Need Input&quot;
Form.password.Focus
Else
Form1_OnSubmit = True
End If
End Function
-->
</SCRIPT>
<BODY>
<form name=Form1 action=ReachOut.asp method=POST>
Name <INPUT type=TEXT name=&quot;UserName&quot;> (Required)<BR>
pass word <INPUT type=TEXT name=&quot;passwOrd&quot;> (Required)<BR>
<INPUT type=SUBMIT value=&quot;Submit Comments&quot;>
</FORM>
</BODY>
</HTML>

or if you want to get more in to it. you can do a onBlur for each field returing the check like this
<html>
<head>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
Function Val(this)
If (this) = &quot;&quot; Then
MsgBox &quot;Please enter something.&quot;&var1, vbCritical, &quot;Need Input&quot;
End If
End Function
-->
</SCRIPT>
<BODY>
<form name=Form1 action=ReachOut.asp method=POST>
Name <INPUT type=TEXT name=&quot;UserName&quot; onBlur=&quot;Val(Form1.UserName.Value)&quot;> (Required)<BR>
pass word <INPUT type=TEXT name=&quot;passwOrd&quot; onBlur=&quot;Val(Form1.password.Value)&quot;> (Required)<BR>
<INPUT type=SUBMIT value=&quot;Submit Comments&quot;>
</FORM>
</BODY>
</HTML>

to go farther I would give the focus to the field in error though.

I help at your own risk, if I brake it sorry! But if I fixed it, let me know.[thumbsup2]
admin@onpntwebdesigns.com
 
Dear Onpnt:
Thank you for your quick response. I prefer the first solution because I have no idea what is onblur event. One more question for you. I used the same strategy to validate the combobox (select), it did not work? Could you show me how to validate empty combobox? if there are nothing selected?

Thanks

haijun
 
vaery minor cahnge. This is not the most efficient way to do things. I the form is large you may want to go other routes, but if it is relativly small this is fine
<head>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
Function Form1_OnSubmit()
Form1_OnSubmit = False
Set Form = Document.Form1

If (Form.UserName.Value) = &quot;&quot; Then
MsgBox &quot;Enter your name.&quot;, vbCritical, &quot;Need Input&quot;
Form.UserName.Focus
elseif (Form.password.Value) = &quot;&quot; Then
MsgBox &quot;Enter your pass word please.&quot;, vbCritical, &quot;Need Input&quot;
Form.password.Focus
Elseif Form.products.Value = &quot;&quot; then
MsgBox &quot;select a product please.&quot;, vbCritical, &quot;Need Input&quot;
Form.products.Focus
Else
Form1_OnSubmit = True
End If

End Function
-->
</SCRIPT>
<BODY>
<form name=Form1 action=ReachOut.asp method=POST>
Products <select name=&quot;products&quot;>
<option value=&quot;&quot;>
<option value=&quot;blah1&quot;>blah1
<option value=&quot;blah2&quot;>blah2
</select>
Name <INPUT type=TEXT name=&quot;UserName&quot;> (Required)<BR>
pass word <INPUT type=TEXT name=&quot;passwOrd&quot;> (Required)<BR>
<INPUT type=SUBMIT value=&quot;Submit Comments&quot;>
</FORM>
</BODY>
</HTML> I help at your own risk, if I brake it sorry! But if I fixed it, let me know.[thumbsup2]
admin@onpntwebdesigns.com
 
My problem was solved under your help. Thanks.
[bigsmile]
Haijun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top