Link to home
Start Free TrialLog in
Avatar of trumps007
trumps007

asked on

VBScript form validation, user hits enter bypassing _OnSubmit event

I'm using the following code to prevent a user from entering a value in a text box higher than that stored in the session.  When the user hits the submit button, the script works properly, but if they hit ENTER while inside that text box the form submits bypassing any form validation...

First, here's the code:

<SCRIPT LANGUAGE = "VBSCRIPT">
Dim frm1
Dim frm2
dim frm3

sub window_onLoad
    set frm1 = document.form
    set frm2 = document.form1
    set frm3 = document.form2
end sub


sub jbno_OnChange
    frm1.submit
End sub
   
sub jaseq_OnChange
    If frm2.jaseq.value = "0" Then
        Alert "You MUST select an operation number!"
    Else
        frm2.submit
    End If
End sub

sub Submitbutton_OnClick
    jaseq = frm3.hours.value
    If frm2.jaseq.value = "0" Then
        Alert "You MUST select an operation number!"
        frm2.jaseq.focus
        Exit Sub
    ElseIf frm3.hours.value = "" or isnumeric(frm3.hours.value) = False Then
   
        Alert "You MUST enter a valid amount of time worked!"

    ElseIf frm3.hours.value > <%=session("total")%> Then
   
        Alert "You cannot enter more then the <%=session("total")%> hours left on the time card!"


            Else
        frm3.submit
    End If
End sub


Sub frm3_OnSubmit
jaseq = frm3.hours.value
    If frm2.jaseq.value = "0" Then
        Alert "You MUST select an operation number!"
        frm2.jaseq.focus
        Exit Sub
    ElseIf frm3.hours.value = "" or isnumeric(frm3.hours.value) = False Then
   
        Alert "You MUST enter a valid amount of time worked!"
    ElseIf frm3.hours.value > <%=session("total")%> Then
   
        Alert "You cannot enter more then the <%=session("total")%> hours left on the time card!"
        frm3_OnSubmit = False

        Else
        frm3.submit
    End If
End Sub
     
</SCRIPT>


Next, if I copied the contents of frm3_OnSubmit to hours_OnChange, it WORKS, generating the alert, but after they hit OK, it still submits the form.  HOW do I prevent the form from submitting?  I've tried all sorts of things like

frm3_onSubmit = False
hours_OnChange = False
Exit Sub

Etc etc.  Basically I get errors using all three methods.  However, the only time the frm3_OnSubmit work is when it is inside the frm3_OnSubmit sub.

Any ideas?

Thanks!

-Jason
ASKER CERTIFIED SOLUTION
Avatar of xabi
xabi

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 knightEknight
Try putting the validation in the onSubmit of the form:

<FORM name='frm3' action="..." method="post" onSubmit='return(frm3_OnSubmit());'>
DOH!  Once again, you beat me to the punch.  :)
Avatar of xabi
xabi

KnightEknight: Sorry ...

xabi

ps: You baby looks pretty at the photo.
Avatar of trumps007

ASKER

hrmz, its not working, it functions just like it did before.  Any other ideas?  I can use the OnChange event to get it to run, but the problem is stopping the form submission in that case...

Thanks!

-Jason
Change your submit button into this onw:

<input type="button" value="Submit" onclick="checkall()">


Then create an script function called checkall() that checks the values, if all goes ok do a document.formname.submit()

xabi
When you click the button, it functions fine, when you hit enter inside the input box, it bypasses the submission.  The current button is of type button and when the OnSubmit() sub is run, it uses frm3.submit to send the contents.....  I'm confused here
Ok, I changed the code to look like this:  (by using the event inside the HTML tag)

<SCRIPT LANGUAGE = "VBSCRIPT">
Dim frm1
Dim frm2
dim frm3

sub window_onLoad
    set frm1 = document.form
    set frm2 = document.form1
    set frm3 = document.form2
end sub


sub jbno_OnChange
    frm1.submit
End sub
   
sub jaseq_OnChange
    If frm2.jaseq.value = "0" Then
        Alert "You MUST select an operation number!"
    Else
        frm2.submit
    End If
End sub

Function submitted()
jaseq = frm3.hours.value
    If frm2.jaseq.value = "0" Then
        Alert "You MUST select an operation number!"
        frm2.jaseq.focus
    ElseIf frm3.hours.value = "" or isnumeric(frm3.hours.value) = False Then
        Alert "You MUST enter a valid amount of time worked!"
    ElseIf frm3.hours.value > <%=session("total")%> Then
        Alert "You cannot enter more then the <%=session("total")%> hours left on the time card!"
        Else
        frm3.submit
    End If
End Function
     
</SCRIPT>



And when they click submit, the error processing works great.  If they have entered a value inside the textbox "hours" and it has focus and they hit enter, the form validation works, and then it submits.  

I just need to know how to stop the form from submitting.

Adjusted points from 50 to 100
Oh, BTW, I'm open to using javascript to do this as well...  :)

-Jason
Hey guys, I ended up turning that field into a populated drop down, but your info gave me some ideas, so I feel its fair to split the points between you.  I e-mailed CS about this.

Thanks!

-Jason
Community Support has reduced points from 100 to 50
Reducing points to allow split.

darinw
Customer Service
Thanks Guys!
Thanks :!)