I have a page with a modal. Inside the modal , i have two buttons. Close and Save Changes. I am trying to add a simple password protected on Save Changes button press. User press button and a pop up with only password. If password is correct continue the rest code. Else alert message.
js:
$('#UpdateForm').submit(function(event) {
if ($pass=="pass") {
echo "Rest Code Here";
} else {
echo "Wrong!";
}
}
I think the code would like like this. How to add pop up and make it work. It is simple protected and no secure as is for local use.
You can use the Window prompt() Method.
The prompt() method displays a dialog box that prompts the visitor for input.
A prompt box is often used if you want the user to input a value before entering a page.
Note: When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. Do not overuse this method, as it prevents the user from accessing other parts of the page until the box is closed.
w3schools.com - Window prompt() Method
You can try the following code:
$('#UpdateForm').submit(function(event) {
y = prompt("Please insert code to continue.");
if (y == "pass") {
alert("Rest Code Here");
} else {
alert("Wrong!");
//Stop closing modal
return false;
}
})
You can use SweetAlert2 to do it
See here https://codepen.io/5hiny/pen/PobNbGQ
Swal.fire({
title: 'Enter password to save',
input: 'text',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
preConfirm: (pass) => {
if (pass == 'password') {
document.write("SAVE");
} else {
document.write("FAIL");
}
},
allowOutsideClick: () => !Swal.isLoading()
})
Related
I have a situation where in, when the user clicks the "back" button, I have to prompt for a "confirm" on a page using JavaScript. The result would decide whether to stay on the page or not.
I have used, the following:
window.onbeforeunload = function () {
var answer = return confirm("Please note by clicking the back button will reset your selection")
if (!answer) {
event.preventDefault();
}
else{
// do something
}
};
Now, even if this prompts a popup on the page, the back button can still be clicked taking the user to the previous page.
On the other hand, by using the following.
window.onbeforeunload = () => {return '';}
I get the message "Changes you made may not be saved.", with the prevention of "back", "forward", "refresh" and all such buttons on the page.
I want this same thing (the prevention of these buttons), to happen on "confirm('message')".
function Confirm_something() {
history.pushState(null, null, location.href);
window.onpopstate = function () {
history.go(1);
};
return confirm("Text you want?");
}
if(Confirm_something){
console.log('user say yes'); //then call your save edit or delete functions.
}
else{
console.log('user say no');
}
I am making in which I want to display alert box and then redirect it to some other page in asp.net
My code is
var s = Convert.ToInt32(Session["id"].ToString());
var q = (from p in db.students
where p.userid == s
select p.sid).SingleOrDefault();
if (q == 0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "reg();", true);
}
this is my reg() function
function reg() {
var con = alert('Please Register Your Self Fisrt ..!! Thank you');
window.location = "reg.aspx";
}
My code is working fine but alert box gets disappear after some second without clicking on it because of that user is unable to read alert message
I want to redirect it to reg.aspx but after clicking OK on alert box ..
please help
Firing a confirm box instead of alert will do what you are trying to achieve. You will have the option to redirect conditionally or unconditionally
function reg(){
var con=confirm("You need to register first before proceeding. Redirect to Registration Page");
if(con==true){
window.location = "reg.aspx";
}
else{
//if redirect unconditionally
window.location = "reg.aspx";
}
}
The scenario is, when I click a button browser would show an alert which accepts user input field with OK and cancel buttons. Now please tell me how to handle this type of alert. As we know CasperJS doesn't displays the alert windows.
This is the casperJS code
casper.then(function () {
this.click('#new-asset > a:nth-child(1)');
casper.setFilter("page.prompt", function(msg, currentValue) {
if (msg === "Choose a filename for your asset") {
return "Firsr.txt";
}
});
});
You can easily solve this by using a Filter in CasperJS. The appropriate one is page.prompt:
// put somewhere before the prompt appears
casper.setFilter("page.prompt", function(msg, currentValue) {
if (msg === "What's your name?") {
return "Chuck";
}
});
Such a dialog is called a prompt (window.prompt()) which is distinct from an window.alert() or window.confirm().
I need a confirmation box to pop up when a user hits a Save button on my webpage. I currently have a confirm box showing up whenever a user hits ANY button. I need it strictly for the one. The button I need it linked to is called btnsavesurvey.
Here is my current code in the page_load:
Dim message As String = "Do you want to complete survey?"
Dim sb As New System.Text.StringBuilder()
sb.Append("return confirm('")
sb.Append(message)
sb.Append("');")
ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())
$('#btnsavesurvey').click(function(){
var r = confirm("Your message");
if (r == true) {
alert("You pressed OK!");
// Do something when the user pressed ok
} else {
alert("You pressed Cancel!");
// Do something when the user pressed cancel
};
});
If your btns id is btnsavesurvey if its class is btnsavesurvey than use .btnsavesurvey
i have disabled the submit button in my guestbook. it has 2 fields[name(textbox)&comment(textarea)].it has 2 other fields ID(primary key) and date.the function is:
function Frmvalidate() {
var nmchk=document.forms["guestform1"]["name"].value;
var cmntchk=document.forms["guestform1"]["comment"].value;
if (nmchk.length==0)
{
var namep = document.getElementById("namep");
namep.innerHTML="name must be filled out";
return false;
}
else if (cmntchk.length==0)
{
var cmntp = document.getElementById("cmntp");
cmntp.innerHTML="comment must be filled out";
return false;
}
else
{
document.getElementById("sbmt").disabled=false;
return true;
}
}
i have called the function in places: body tag's onload,button tag's onclick. still its not working and blank entries are being stored in my database.
You dont need to disable the submit button
you gain noting from it. ( alerting the user , running another script etc...)
instead -- The submit button should stop its regular behaviour by this code :
<input type="submit" onclick="return Frmvalidate();"/>
meaning :
when you press the button , it will execute the function yielding True or False and if it's True (only) it will continue to the server.