Solved

Using Script to errorcheck HTML form

Posted on 2006-06-13
9
218 Views
Last Modified: 2010-04-06
Hi Experts

I have an ASP page where on submit I want to check to see whether a certain field has a value, if not return a message to force the user to enter one.

I have a simple java script:

function error_check(theForm) {
if (theForm.txtInvNo.value == "") {
    alert("Please enter your Invoice Number");
    theForm.txtInvNo.focus();
    return (false);
  }    
return(true);
}

Which I am calling in the header of the HTML page. My question is how can I work the instruction to execute the script into my submit string which goes as follows;
<td nowrap colspan="2"><input type="submit" name="btnUpdate" value="Invoice" class="button">

All help greatly appreciated

Thanks RangerLad
0
Comment
Question by:RangerLad
  • 4
  • 3
  • 2
9 Comments
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 125 total points
ID: 16896432
RangerLad,

Use the form tags onSubmit attribute to cause the script to be run.  In the sample below the function named Form1_Validator is run when the submit button is pushed.  I hope that you are also doing server side validation with ASP. :-)

<form method="POST" action="submit.asp" onsubmit="return Form1_Validator(this)" name="Form1">

b0lsc0tt
0
 

Author Comment

by:RangerLad
ID: 16896489
Sorry, bit new to this game. If I add a variant of your suggestion in addition to my existing submit string how will this get picked up ?

Thanks
0
 
LVL 1

Accepted Solution

by:
cali_oo7 earned 125 total points
ID: 16897902
for your example, you would specifically want to use the following:
<form method="POST" action="submit.asp" onsubmit="return error_check(this)" name="Form1">

when the form is submitted, it first sends the values to error_check() for your to evaluate. Then, after your done evaluating in error_check(), you can return true or return false. If you return true, then the form will continue to be submitted to "submit.asp", if you return false, the form will not be submitted.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 16897986
In case you need some more information than Cali provided then in the example I provided there would be a script right before the form tag.  If we used your script it would look something like this.

<script language="JavaScript" type="text/javascript">
<!--
function error_check(theForm) {
if (theForm.txtInvNo.value == "") {
    alert("Please enter your Invoice Number");
    theForm.txtInvNo.focus();
    return (false);
  }    
}
//-->
</script>
<form method="POST" action="submit.asp" onsubmit="return error_check(this)" name="Form1">

I hope this example helps more.  Let us know if you still have a question or need clarification.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Expert Comment

by:cali_oo7
ID: 16898024
bOlscOtt, you did not add any new helpful information, you just copied my suggestion and his code. Stop hawking over a correct solution to try and steal points. Its unethical.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 16898089
@Cali - Please look to see who responded first!  I'm sorry if it seems like I was trying to steal your points??!  I'm sure that if your comment has everything that Ranger needs to answer his question then he will award you full points and not "let me steal points."  I think you underestimate his ability.  I am fine with him deciding what was helpful.  By the way I didn't just copy your comment.  Personally I think it needed clarification and I didn't use ANY of your comment in my last comment.
0
 
LVL 1

Expert Comment

by:cali_oo7
ID: 16898159
B0lsc0tt, I saw that you responded first, but you responded with an inadequate and frankly juvenile answer which needed to be clarified per Ranger. Im sure you are capable of scrolling up and seeing that. Furthermore, I do not doubt Rangers ability to award points based upon a correct answer, I just question your morals of trying to add a comment that is essentially just copying what I have done. Specifically, you took <form method="POST" action="submit.asp" onsubmit="return error_check(this)" name="Form1"> from my posting and just reposted it with code that Ranger had already written. Be original and ethical and you shall be rewarded.
0
 

Author Comment

by:RangerLad
ID: 16900509
Guys,

Thank you both for your help. Between you I got to where I wanted to and got the Error check to run perfectly. It would be fair to say I did need both responses to get the syntax correct so points split accordingly.

Have a great day
RangerLad
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 16904003
Your welcome!  I'm glad that I could help.  Thank you for the grade, the points and the fun question.

bol
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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.

706 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

18 Experts available now in Live!

Get 1:1 Help Now