alvinchua Posted May 12, 2007 Share Posted May 12, 2007 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.. Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 12, 2007 Share Posted May 12, 2007 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 Quote Link to comment Share on other sites More sharing options...
alvinchua Posted May 12, 2007 Author Share Posted May 12, 2007 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> Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 12, 2007 Share Posted May 12, 2007 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? Quote Link to comment Share on other sites More sharing options...
paul2463 Posted May 12, 2007 Share Posted May 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
alvinchua Posted May 12, 2007 Author Share Posted May 12, 2007 hmmm... thanks alot .. i knoe where is da prob ... class = "box" .. i remove it then ok liao .. anywayz.. thanks alot for the help ~! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.