How to put Yes / No option in Javascript alert?

Hi,

I am checking whether a textbox has value of zero or not.

If its zero, it should display an alert asking user if they want to enter a non-zero value or not.
If user select yes, then cursor should be placed on that textbox.
If not, then it should do nothing.

My current code (see code section) displays error message but it does not have yes/no option.

How do I achieve that?


if (document.getElementById('ctl00_PageContent_txtEmployment_n0').value == '0' || document.getElementById('ctl00_PageContent_txtEmployment_n0').value == '') {
            alert('Are you sure Point 4 Tax = 0 ?  If not, please enter other value in the textbox.');
            return false;

Open in new window

meetpdAsked:
Who is Participating?
 
kadabaConnect With a Mentor Commented:
why not this way?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Form Test</TITLE>
<script>
	function validateValue()
	{
		var txtObj = document.getElementById('ctl00_PageContent_txtEmployment_n0');
		if (txtObj.value == '0' || txtObj.value == '') 
		{
            var ans = confirm('Are you sure Point 4 Tax = 0 ?  If not, please enter other value in the textbox.');
            if(ans)
			{
				txtObj.focus();
			}
		}
	}	
</script>
</HEAD>
<BODY>
<input type="text" id="ctl00_PageContent_txtEmployment_n0"><br>
<input type="button" value="check value" onclick="validateValue();"><br>
</BODY>
</HTML>

Open in new window

0
 
Gurvinder Pal SinghCommented:
You can't achieve it using javascript.
You either need to use VBscript or create a custom dialog box (a div) which will have those buttons and text.

In VbScript, do something like

intAnswer = _
    Msgbox("Do you want to delete these files?", _
        vbYesNo, "Delete Files")

If intAnswer = vbYes Then
    Msgbox "You answered yes."
Else
    Msgbox "You answered no."
End If

http://www.w3schools.com/VBScript/func_msgbox.asp
http://www.devguru.com/technologies/vbscript/QuickRef/msgbox.html
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Gurvinder Pal SinghCommented:
However, if you want to show a custom alert box, refer to this article
http://www.xploredotnet.com/2007/04/custom-alert-box-using-javascript.html

0
 
deepu chandranCommented:
hi,

You can use below div also for confirm operation, this will be more user friendly and managable,
<div id="Confirm" runat="server" style="width: 450px; height: 135px; position: absolute;
            left: 178pt; top: 200pt; background-color: white;">
            <table width="100%" style="height: 100%; border-style: solid; border-color: Black;
                border-width: thin;">
                <tr>
                    <td align="left" class="header" style="height: 4px">                        
                        <asp:Label ID="lblDlgHeading" runat="server" Text="Falcon-i"></asp:Label></td>
                </tr>
                <tr>
                    <td valign="top" align="center" style="height: 53px">
                        <table style="width:100%">
                            <tr>
                                <td style="width:35px;">
                                    <asp:Image ID="imginfo" runat="server" ImageUrl="~/Images/32X32/Info.png" />
                                </td>
                                <td>
                                    <asp:Label ID="lblCloseTripMsg" runat="Server" Font-Bold="True"></asp:Label>
                                </td>
                            </tr>
                        </table>                        
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="ButtonStyle" OnClick="btnYes_Click" />&nbsp;
                        <asp:Button ID="btnNo" runat="server" Text="No" CssClass="ButtonStyle" OnClick="btnNo_Click" />
                    </td>
                </tr>
            </table>
        </div>
0
 
leakim971PluritechnicianCommented:
Hello meetpd,

You need to use : confirm(message)

ref : http://www.w3schools.com/js/js_popup.asp
if(document.getElementById('ctl00_PageContent_txtEmployment_n0').value == '0' || document.getElementById('ctl00_PageContent_txtEmployment_n0').value == '')
   answer = confirm('Are you sure Point 4 Tax = 0 ?  Please click cancel to enter other value in the textbox.');
// answer = false if the user click on cancel !false = true
if(!answer) {
   // Clear if we have zero
   document.getElementById('ctl00_PageContent_txtEmployment_n0').value = "";
   // Put focus on textbox
   document.getElementById('ctl00_PageContent_txtEmployment_n0').focus();
}

Open in new window

0
 
kadabaCommented:
@leakim971:

I have already suggested that, perhaps you should go through the posts. Perhaps if the user does want Yes or No you could use the div option suggested earlier.

0
 
leakim971PluritechnicianCommented:
Sorry kabada (last post seen before my post was ID:26111778)
My phone ring, lot of blabla, I write my solution, take my time... bla bla bla... LOL

I APOLOGIZE !
0
 
kadabaCommented:
>> I APOLOGIZE !
No leakim, I did not mean to be rude. apologies if you felt so.
0
 
leakim971PluritechnicianCommented:
No worries, UPPERCASE just for fun ;-) I know you have a good spirit bro'
0
 
meetpdAuthor Commented:
Hi,

The attached code mentioned above works. Only problem is : if we click Cancel button, the focus comes on to the textbox but the page is redirected to next page.

What should I do so that the control stays there?

Currently we are checking the above textbox when the user presses the next button.

Thanks!
if(document.getElementById('ctl00_PageContent_txtEmployment_n0').value == '0' || document.getElementById('ctl00_PageContent_txtEmployment_n0').value == '') 
   answer = confirm('Are you sure Point 4 Tax = 0 ?  Please click cancel to enter other value in the textbox.'); 
// answer = false if the user click on cancel !false = true 
if(!answer) { 
   // Clear if we have zero 
   document.getElementById('ctl00_PageContent_txtEmployment_n0').value = ""; 
   // Put focus on textbox 
   document.getElementById('ctl00_PageContent_txtEmployment_n0').focus(); 
}

Open in new window

0
 
RajasekaranMoorthyCommented:
Click cancel button just return false. So the page wont be post back

Javascript Function:

function ConfirmMsg()
    {
   if(document.getElementById('txt1').value == '0' ||
    document.getElementById('txt2').value == '')
    answer = confirm('Are you sure Point 4 Tax = 0 ?  Please click cancel to enter other value in the textbox.');
// answer = false if the user click on cancel !false = true
    if(!answer) {
       // Clear if we have zero
       document.getElementById('txt1').value = "";
       // Put focus on textbox
       document.getElementById('txt1').focus();
       return false;
    }
    return true;
 }

Server Control:

<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    <asp:Button ID="btn" runat="server" OnClientClick="return ConfirmMsg();" Text="Submit" />
0
 
meetpdAuthor Commented:
Thanks RajasekaranMoorthy,

One problem: Next button already has one OnClientClick function.

How to attach this function in that case?

0
 
RajasekaranMoorthyCommented:
We can call this function within Next button OnClientClick function.

For example, OnClientClick in Next button Contains below javascript function

function NextButtonFuntion()
{
bool bFlag = false;
bFlag = ConfirmMsg(); // Another Javascript function

if(!bFlag)
    return false;
else
 //Some Code related to Next button event
}
Server Control:
<asp:Button ID="btnNext" runat="Server" OnClientClick="return NextButtonFuntion();" />

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.