Solved

Using Script to errorcheck HTML form

Posted on 2006-06-13
9
219 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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