Solved

Using Script to errorcheck HTML form

Posted on 2006-06-13
9
226 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
[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
  • 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
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 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
 
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

Industry Leaders: 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!

Question has a verified solution.

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

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 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

734 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