Link to home
Start Free TrialLog in
Avatar of bonnseye

asked on

Validation In The ONCLICK() Event

Hello All,
I have a Image which is acting as a button on one of my asp page, like

<img src="images/buy.jpg" name="buy" width="38" height="19" border="0" id="buy" OnClick="window.location.href='viewcart.asp?colour='+document.getElementById('select2').value" style="cursor:pointer;">

But on the click of this image button I want to validate also some variable value before proceeding to Viewcart.asp page. If that particular variable value is TRUE I need to proceed to Viewcart page as above otherwise Display an Alert Message and not to proceed & remain on that page as it is.

Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Try this,
<input type="image" src="images/buy.jpg" name="buy" width="38" height="19" border="0" id="buy" OnClick="window.location.href='viewcart.asp?colour='+document.getElementById('select2').value" style="cursor:pointer;" />

Open in new window

then you need to validate it in the function,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function checkValue() {
	if (validate =true) {
	} else {
		return false;
<input type="image" src="images/buy.jpg" name="buy" width="38" height="19" border="0" id="buy" OnClick="javascript:checkValue();" style="cursor:pointer;" />

Open in new window

Avatar of bonnseye



but the validation is something where I need to use the asp code, it is something like
<% if trim(request.Cookies("proceed"))=true then %>

OK, can you do like this,?
<% if trim(request.Cookies("proceed"))=true then %>
<input type="image" src="images/buy.jpg" name="buy" width="38" height="19" border="0" id="buy" OnClick="window.location.href='viewcart.asp?colour='+document.getElementById('select2').value" style="cursor:pointer;" />
<% Else %>
<input type="image" src="images/buy.jpg" name="buy" width="38" height="19" border="0" id="buy" OnClick="javascript:alert('This is test alert msg')" />
<% End If %>

Open in new window

just response.write it ( input type image )
Thanks logudotcom:
I would have tried it at the first place. But you know whats my prob is that I have used
...... FOR VARIABLE <<proceed>>........

and actually this variable proceed is changing its value even on the same page itself without reloading using iframe, whenever the user selects some size from the SELECT2 ComboBox (which is actually a shoe-size combo-box) and is made like-

<select name="select2" ....... onchange='document.getElementById("iframe1").src="find.asp?size="+ this.value'>

and therefore find.asp is changing the PROCEED variable value depending on the size selected that is whenever the user changes the size combo. So in a nutshell it is not Fixed on the same page itself otheriwse very simply I could have used your second solution.

mmm ., Is it possible to post the complete code?
Thanks logudotcom:

Actually there is nothing left as such to tell you in the code, Buy button code is already there and select2 statement is also there, select2 OnChange is invoking find.asp, which is changing a cookie variable Name PROCEED each time it is selected.
And finally Buy Button Click depends on this Procced Value otherwise just Alert message.

Actually you see this link

here on this page just select the value from the SIZE COMBO
6 or 7 or 8 or ......, you will see a field is changing just beneath it which is QTY, you can take that way that Buy Button Clicks Depends upon that.

mmm, no idea.
but if you trying to change the value on qty based on <select ... then look at this,
Thanks logudotcom:
Thanks for the link, it was good.

If you still have a question or need help with this then let us know.  I'll admit I am unclear on somethings you describe and also think some more code would may help make sense of it.  However for now clarifying where this validation code should be may be enough.  What file is it in or need to be in?  To keep them clear also which file is the select in and are any other files involved?
Is this all part of a form (or whatever) that is already going to ViewCart.asp OK or is that the problem?
Hopefully the expert help above is all you need though and has taken care of this.  Let us know if that isn't the case though.
Thanks b0lsc0tt:
My problem is still not solved, its just that logudotcom was posting some possible solutions and I was clearifying him thats its not possible in my case. I explain you, you can also open the foll. page

files involved are
moreinfo2.asp, viewcart.asp and find.asp
Everything is in moreinfo2.asp, viewcart.asp is a asp where you are taken to when a button BUY is clicked on moreinfo2, so not bothered and also find.asp is an asp which is changing a cookie value named PROCEED and is invoked on OnChange of a combo Select2 on the same page moreinfo2.asp.

So we are only concerned about moreinfo2.asp and here only we need to put the validation, no form just a combo box Select2 and am Image Button as-

<select name="select2" ....... onchange='document.getElementById("iframe1").src="find.asp?size="+ this.value'>
user changing select2 values Invoke find.asp which in turn changes the value of a cookie PROCEED
<% trim(request.Cookies("proceed"))=true/false %>

Buy button on moreinfo2.asp is

ONLY thing needed is Buy button when pressed should go to viewcart.asp, if Cookie PROCEED is TRUE and if FALSE Should remain on the same page moreinfo2 page and just an alert message.

Now finally logudotcom told 1st to use OnClick="javascript:checkValue();" in the Buy button and put the validation there, but since the validation is related to asp i.e., <% if trim(request.Cookies("proceed"))=true then %>, I cannot do so. Then the 2nd and his final suggestion was to use simply

<% if trim(request.Cookies("proceed"))=true then %>
MAKE A BUY Button of ur choice
<%else if trim(request.Cookies("proceed"))=false then%>
MAKE ANOTHER BUY Button of ur choice

but that was not possible as PROCEED cookie value is not fixed at the starting of the page, it changes on the same page whenever user select something or ther other from the combo box select2.

Thanks for clarifying.  It helped.
The only way to check the cookie's value BEFORE going to the viewcart.asp page is with Javascript code.  However why don't you have viewcart.asp check at the beginning of its script.  With the way to do this that is how I would do it.  Checking and working with cookies is easier in ASP and would be more reliable than Javascript to do it (since the Javascript could be disabled or possibly not supported).  Some ASP lines like the following at the beginning of viewcart.asp would be all you need to check this ...
If Request.Cookies("Proceed") <> "True" then
    Response.Write "Please go back and complete the form."
End if
This method would go to the next page and not pop up an alert but fits better with your other code that depends on ASP and will be more reliable.
If you want to stick with Javascript, which is actually required for the cookie to even be made, then I would consider simplifying what you are doing.  Do you need the cookie for anything else or is it just being used to know the select was changed?  It seems like you just use the cookie for this so I would change that.  Use a global Javascript variable.  For example at the start of your page in the head section have ...
<script type="text/javascript">
var proceed = false;
Then use the following value in the onchange event in the select tag.
onchange="proceed = true;"
If you need to still use find.asp for something else then you could but this would avoid the unneeded cookie.  Then when the button is clicked you can use ...
onclick="if (!proceed) { alert('You can't go.'); return false; } else { window.location.href='viewcart.asp?colour='+document.getElementById('select2').value; }" style="cursor:pointer;">
That will provide your javascript alert if the value wasn't changed.  If you have to stick as is but want Javascript to read the cookie then start by looking at and adding the scripts from .  They are great for working with cookies in Javascript.  If all you need is to read the cookie then you can just get that script.  I can help if you have a question working with it in your page but the info and scripts there are the best I have seen for working with a cookie in Javascript.  I still recommend against this method but you do have this info if you have to use it.
Let me know how this helps or if you have a question.
Thanks b0lsc0tt for a detailed reply
(1) Idea 1 seems fine that I go to Viewcart.asp page and put few ASP lines at the starting of the page as a check then depending upon Request.Cookies("Proceed") value should show the page Otherse No Content only a BACK Button to move back, but initially only, the requirement was such that we needs to remain on the moreinfo2 that is the same page and an ALERT on Request.Cookies("Proceed")=FALSE and Viewcart Page on Request.Cookies("Proceed")=TRUE.
(2) I am not saying that proceed is True just on a touch of SELECT2, that is select was changed. I am not tracking select with PROCEED Cookie, so I cannot write in Select2 <select> tag
onchange="proceed = true;"
OnChange of Select2, FIND.ASP needs to be run with select2 value that is THIS.VALUE, and there it opens the database with the select2.value, checks if the stock is >0 or <=0 for this.value passed and makes <%Response.Cookies("proceed")  = TRUE OR FALSE %> respectively.
So instead of a JavaScript Variable Proceed on the moreinfo2 page I really need the Request.Cookies("Proceed") , which has to be accessed OnClick of BUY button for validation.
In the above scenario do you suggest me to read or some other suggestion as well.

Avatar of b0lsc0tt
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
b0lsc0tt sorry for the delay.

No worries!  Thanks for coming back to close this, for the grade, the points and the fun question.  I'm glad I could help.