Solved

VBScript form validation, user hits enter bypassing _OnSubmit event

Posted on 2000-03-15
15
952 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 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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/…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now