Solved

VBScript form validation, user hits enter bypassing _OnSubmit event

Posted on 2000-03-15
15
962 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +1
15 Comments
 
LVL 8

Accepted Solution

by:
xabi earned 50 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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