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
LVL 1
trumps007Asked:
Who is Participating?
 
xabiCommented:
you must create the form in this way:

<form name="...." action="...." method="..." onsubmit="return(myfunctioncheckname())">

And the function must return true if all is ok or false if something goes wring. This will work when you press Enter or simply presss the submit button.

xabi
0
 
knightEknightCommented:
Try putting the validation in the onSubmit of the form:

<FORM name='frm3' action="..." method="post" onSubmit='return(frm3_OnSubmit());'>
0
 
knightEknightCommented:
DOH!  Once again, you beat me to the punch.  :)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
xabiCommented:
KnightEknight: Sorry ...

xabi

ps: You baby looks pretty at the photo.
0
 
trumps007Author Commented:
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
0
 
xabiCommented:
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
0
 
trumps007Author Commented:
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
0
 
trumps007Author Commented:
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.

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

-Jason
0
 
trumps007Author Commented:
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
0
 
darinwCommented:
Community Support has reduced points from 100 to 50
0
 
darinwCommented:
Reducing points to allow split.

darinw
Customer Service
0
 
trumps007Author Commented:
Thanks Guys!
0
 
knightEknightCommented:
Thanks :!)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.