Jump to content

[SOLVED] javascript Are you Sure ???


alvinchua

Recommended Posts

i need to display a alertbox "are you sure?" to allow user to confirm... and my onclick function adi use to validate the blank space in the textbox .. and this is the code below

 

<script language="JavaScript">
function checkForm()
{
var amountpay;

with(window.document.ptenagafrm)
{
	amountpay= amount;
}

if(trim(amountpay.value) == '')
{
	alert('Please enter the amount');
	amountpay.focus();
	return false;
}
else
{
	amountpay.value = trim(amountpay.value);
	return true;
}
}

function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}

</script>


and this is the button..

and this is the submit button <th></th><td height="50"><input name = "enter" id = "enter" type ="submit" align ="center" value = "Submit" onclick="return checkForm();">

 

if everything is fine and i click on the submit button it will display "Are you Sure?" if ... ok .. it will insert data into the database and if cancel it will not.. please advice..

Link to comment
Share on other sites

I think I know what you want and here ismy effort for it

function checkForm()
{
var amountpay;
var returnvar = false; /* another variable to hold the false statement*/

with(window.document.ptenagafrm)
{
	amountpay= amount;
}

if(trim(amountpay.value) == '')
{
	alert('Please enter the amount');
	amountpay.focus();
}
else
{
	amountpay.value = trim(amountpay.value);
	returnvar true; /*sets the returnvar to true if everything is ok*/
}
if (returnvar == true) /*if it has been set to true everything is ok */
{
	window.confirm("Are You Sure"); /*automatically returns true or false for you*/
}
else
{
	return returnvar; /*otherwise send back false*/
}
}

 

hope thats a help anyway

Link to comment
Share on other sites

i have change according to what you say ... but ... it seem to be not working ~~ pls advice...

 

 

<script language="JavaScript">
function checkForm()
{
var amountpay;
var returnvar = false;

with(window.document.ptenagafrm)
{
	amountpay= amount;
}

if(trim(amountpay.value) == '')
{
	alert('Please enter the amount');
	amountpay.focus();
	return false;
}
else 
{
	amountpay.value = trim(amountpay.value);
	returnvar = true;
	return true;
}

if (returnvar == true) /*if it has been set to true everything is ok */
{
	window.confirm("Are You Sure"); /*automatically returns true or false for you*/
}
else
{
	return returnvar; /*otherwise send back false*/
}

function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}
}


</script>

Link to comment
Share on other sites

function checkForm()
{
var amountpay;
var returnvar = false; 

with(window.document.ptenagafrm)
{
	amountpay= amount;
}

if(trim(amountpay.value) == '')
{
	alert('Please enter the amount');
	amountpay.focus();
}
else
{
	amountpay.value = trim(amountpay.value);
	returnvar true; /*sets the returnvar to true if everything is ok*/
}
if (returnvar == true) 
{
	returnvar = window.confirm("Are You Sure"); 
	return returnvar;
}
else
{
	return returnvar;
}
}

function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}

Leave the function trim() as a seperate function toy our checkform() function, I have just checked the if(returnvar==true) part on my machine and changing the values it works perfectly, if you are going to ammend your code with mine type it as it is, in your last post you have left all sorts of lines in there that make it not work. did your code work before my adjustments, such as checking the fields?

Link to comment
Share on other sites

there were a few faults with your code, I have run and tested this code, I have assumed that the input box for amount has an ID of amount such as

 

<input type="text" id="amount" name="amount">

 

if so then copy and paste this code into the page

 

<script language="JavaScript">
function checkForm()
{
var amountpay = document.getElementById('amount').value;
var returnvar = false; 

if(trim(amountpay) == '')
{
	alert('Please enter the amount');
	document.getElementById('amount').focus();
}
else
{
	document.getElementById('amount').value = trim(amountpay);
	returnvar = true;
}
if (returnvar == true) 
{
	returnvar = window.confirm("Are You Sure?"); 
	return returnvar;
}
else
{
	return returnvar;
}
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

</script>

 

this code now takes in what was typed into the text box, if its empty is alerts you to enter an amount and stops the post, if there is an amount there it asks are you sure, if no it stops the post if yes it allows the post with the trimmed value of the text box put back into the text box to be posted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.