Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Script to errorcheck HTML form

Posted on 2006-06-13
9
Medium Priority
?
229 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 500 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

688 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