Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

custom error messages

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I have a submit button when pushed inputs data into a database. I also have a checkbox where the user clicks if they agree. I want to make sure they clicked the checkbox before entering the data in the database. I was wondering how would I do something like this.

If the box is checked then

data would be entered.

If the checkbox is not selected then

Error message "you must check the box" // the screen would display this message.

I just don't know the syntax for doing the error. I have no problem with stuff entering the database, but I need a custom error. I would use javascript but this page reloads with info 3 times before the actual submit button appears for the user.

Any help would be great!
 
Even though the button is not available to the user until later your best option by far is to use JavaScript. Ideally when creating HTML based forms you should try and do as much of the validation on the client side. After all why use your own processing power when you can use theirs.

If you use a .js file for your JavaScript just include the Script in that to either prompt them to accept when they attempt a submission or as part of other validation.

HTH
William
Software Engineer
ICQ No. 56047340
 
Maybe something like this:

<script language=javascript>
function validateMe(){
var output;
if (document.theForm.theBox.checked)
output = true;
else{
alert('You must check the box first');
output = false;
}
return output;
}
</script>

<form name=theForm method=post action=somePage.asp onSubmit=&quot;return valdiateMe();&quot;>
<input type=checkbox name=theBox value=1>
<input type=submit value=Submit name=submit1>
</form>

so that you call the function onSubmit of the form, and the function returns true (thereby allowing the form to submit) if the box is checked, or fires an alert(), and returns false (thereby not allowing the form to submit) if the box is not checked.

:)
paul
penny1.gif
penny1.gif
 
when they first hit submit on the page they don't even see this checkbox so wouldn't they still get an error or a null error or saying the object doesn't exist? Infact only certain users ever see this checkbox, but if a user does see it then I have to validate they checked it. Here is how my code works.

<form1 actions=&quot;the same document&quot; onsubmit =&quot;checkit()&quot;>

<% if first drop down menu is = &quot;&quot; Then %>
Drop down menu with submit button
<% End If %>

<% if first drop down menu is <> &quot;&quot; and second dropdown is = &quot;&quot; Then %>
Second dropdown with submit button
<% End If %>

<% if both of the above boxes is <> &quot;&quot; Then %>
Table shows here
<% if access level is equal to &quot;2&quot; Then %>
Show Checkbox for approval
<% End If %>
First submit button would be here.
<% if access level is equal to &quot;2&quot; then
show approval button. This button is where the error needs to accure if the check box is not selected. and they tried to submit.
<% End If %>
<% End If %>
<form>
Page ends

 
you could use the same logic by which you show your checkbox to determine whether or not you'll check for it:

<script language=javascript>
function validateMe(){
var output = true;
<%if yourCheckboxLogicHere then%>
if (!(document.theForm.theBox.checked)){
alert('You must check the box first');
output = false;
}
<%end if%>
return output;
}
</script>

so the meat of the function doesn't get written unless you also write the checkbox... and if the checkbox isn't shown, then true automatically gets returned.

if the checkbox logic evaluates to false, then the function will render like so:

<script language=javascript>
function validateMe(){
var output = true;
return output;
}
</script>

penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top