Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBScript form validation, user hits enter bypassing _OnSubmit event

Posted on 2000-03-15
15
Medium Priority
?
970 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:trumps007
  • 7
  • 3
  • 3
  • +1
15 Comments
 
LVL 8

Accepted Solution

by:
xabi earned 200 total points
ID: 2620263
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 2620270
Try putting the validation in the onSubmit of the form:

<FORM name='frm3' action="..." method="post" onSubmit='return(frm3_OnSubmit());'>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2620272
DOH!  Once again, you beat me to the punch.  :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Expert Comment

by:xabi
ID: 2620290
KnightEknight: Sorry ...

xabi

ps: You baby looks pretty at the photo.
0
 
LVL 1

Author Comment

by:trumps007
ID: 2620326
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
 
LVL 8

Expert Comment

by:xabi
ID: 2620376
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
 
LVL 1

Author Comment

by:trumps007
ID: 2620420
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
 
LVL 1

Author Comment

by:trumps007
ID: 2620489
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
 
LVL 1

Author Comment

by:trumps007
ID: 2620497
Adjusted points from 50 to 100
0
 
LVL 1

Author Comment

by:trumps007
ID: 2620498
Oh, BTW, I'm open to using javascript to do this as well...  :)

-Jason
0
 
LVL 1

Author Comment

by:trumps007
ID: 2620856
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
 
LVL 3

Expert Comment

by:darinw
ID: 2621093
Community Support has reduced points from 100 to 50
0
 
LVL 3

Expert Comment

by:darinw
ID: 2621094
Reducing points to allow split.

darinw
Customer Service
0
 
LVL 1

Author Comment

by:trumps007
ID: 2621109
Thanks Guys!
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2621468
Thanks :!)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question