Solved

Using Script to errorcheck HTML form

Posted on 2006-06-13
9
220 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP PDO get the error if exists 3 88
Lines Not Lining Up 2 70
website content maintenance 3 94
Re-position sub-options beneath the TAB 7 88
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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