So I have a "create an account" form with an age input. I have the age requirement set to 16. What I want to do is if the person is under the age of 16 it gives an alert message, and then when the user clicks the OK button on the alert window it sends them back to the index page.
Here is what I have for JS code:
if (age == null || age == "")
{
alert("Please enter your age to continue.");
return false;
}
else if (age == isNaN(age))
{
alert("Age input was not a number!\nPlease enter your age to continue.");
return false;
}
else if (age < 16)
{
alert("You are not old enough to have an account.\n If you have concerns or questions about our age policy please send them to ghost565xster#gmail.com.");
return false;
}
Basically when it runs that last "else if" statement and finds the input was less than 16, I want it to redirect the user to the index.html (Home) page after displaying the alert message.
I have tried using:
else if (age < 16)
{
alert("You are not old enough to have an account.\n If you have concerns or questions about our age policy please send them to ghost565xster#gmail.com.");
location.assign("index.html");
return false;
}
I have also tried location.href but neither work. Any help on this will be greatly appreciated!
Just assign a new value to the location property. location = "index.html"
this What's the difference between window.location= and window.location.replace()?
Best practice since you dont want them pressing back and getting in to a loop would be to use window.location.replace("sorryYouAreJustTooYoung.html");
Use this:
window.location.href = "your location";
You should post your html content too as you may have a problem there. I had this problem some time ago, when the age was under the required age limit i was directed to a site i never specified with the window.location.href.... even though it was assigned to a different site, the problem may be that you have wrapped an anchor tag around it so it will direct you to the site that you specified on your html page, i suggest you check that you dont have an anchor tab directing you elsewhere on your html page!
easiest as possible:
window.location = "http://www.google.com/"
Related
I currently have a script for blocking non-business email addresses on my website form (Marketo) yet bots are still finding a way to bypass it. Today I got another "gmail" and "hotmail.fr" submission. Whenever I test my form it works but the bots are finding a way to bypass the script. I have also tried the honeypot method but it has not worked. They are also submitting with two-letter names, the past month has been really bad and I am desperate for help, I am not very good in JS so any help would be very much appreciated.
This is my script:
(function (){
// Please include the email domains you would like to block in this list
var invalidDomains = ["#gmail.","#yahoo.","#hotmail.","#live.","#aol.","#outlook.","#icloud.","#zoho.","#hubspot.","#gmx.","#yandex.","#mail.","#email.","#tutanota.","#trashmail.","#lycos.","#tutanota.","#protonmail."];
MktoForms2.whenReady(function (form){
form.onValidate(function(){
var email = form.vals().Email;
if(email){
if(!isEmailGood(email)) {
form.submitable(false);
var emailElem = form.getFormElem().find("#Email");
form.showErrorMessage("Must be Business email.", emailElem);
}else{
form.submitable(true);
}
}
});
});
function isEmailGood(email) {
for(var i=0; i < invalidDomains.length; i++) {
var domain = invalidDomains[i];
if (email.indexOf(domain) != -1) {
return false;
}
}
return true;
}
})();
</script>```
Some bots when they run, they don't even execute the JavaScript on the page. They just take the fields, find the post request the form is submitting, and submit the fields to them with pre-defined values. Thus, ignoring your validation completely.
So now the solution would be checking the post request values on the backend. Some people use the fact that some bots are dumb, and they include a honeypot field in their fields. They mark it hidden with CSS on the frontend, but again, some bots are dumb, and they will fill it out regardless and send it in the post request. Now you can have your sever throw that out right away.
Can I set a time limit (for example in 8 hours you have to retype the password)?
Here's my very simple javascript:
<SCRIPT language="JavaScript">
<!--hide
var password;
var pass1="x10Kz4iz4ZvEB2wgUBA5otc1";
var pass2="x10Kz4iz4ZvEB2wgUBA5otc1";
var pass3="x10Kz4iz4ZvEB2wgUBA5otc1"
password=prompt('Please enter your password to view this page!',' ');
if (password==pass1 || password==pass2 || password==pass3)
alert('Password Correct! Click OK to enter!');
else
{
alert('Uh oh try again');
window.location="#";
}
//-->
</SCRIPT>
I have this under the head tag of my HTML text.
Thank you :)
This is not really a secure way of protecting your website because anyone can look at the source code of a page and see the Javascript along with the passwords you wrote there.
However, for learning purposes, you might want to learn about setting and getting cookies (How do I create and read a value from cookie?). You can store the time the user logged in, and next time check if it has been eight hours (just compare the time and date of last login to current login time and date).
Also, to get the current time, you should read up on the Javascript Date() function.
I cannot seem to find the answer to this anywhere. The closest I have managed to do is to password protect a page with php, but every solution I've found in that regard I haven't been able to get to work properly. Basically I am making a web-based puzzle game. I have the current version set up with javascript to forces you to enter a password to get to the next level. I would like something similar in php because you can easily view the password if you look at the page source. I really like what this js code does, unfortunately I can't find any resources how to do that with php. Any help would be appreciated.
<SCRIPT>
function passWord() {
var testV = 1;
var pass1 = prompt('Enter the Code',' ');
while (testV < 3) {
if (!pass1)
history.go(-1);
if (pass1.toLowerCase() == "1234") {
alert('It came unlocked!');
window.open('sample-level.htm');
break;
}
testV+=1;
var pass1 =
prompt('That is not the correct answer.','Password');
}
if (pass1.toLowerCase()!="password" & testV ==3)
history.go(-1);
return " ";
}
</SCRIPT>
I just need to be able to protect the next destination page, not the current one.
You need to use PHP sessions. You'll need to create a Database, create a table in that database to store the username, password and perhaps email. Then you'll want to create a file to connect to the database. Include that file in your login/password form. All the code and information is a bit much to put here but the link below is very strait forward tutorial and should get you started.
http://code.tutsplus.com/tutorials/user-membership-with-php--net-1523
So i'm usually pretty good at figuring out these types of problems but this one has got me completely stumped as have been trying to fix for about 6 hours now.
i have a web form which contains a username check using jquery script and a php script. it was working fine when i first wrote the code and now has completely stopped working(no idea why). The php script is a pretty standard mysqli query which returns the numrows and then i either echo 1 or 0 to the page depending on if a result was found. this script works fine and have tested it independently and echos 1 if no result was found and 0 if a result was found.
in the jquery script i have a min value check and a no value check which both work fine. Then my ajax call and i receive the result with success:function(data). this also has been tested independently by printing the result(data) on screen and prints a 1 on screen when there is no record and a 0 on screen when a record if found so i know everything is working fine with the sent data and im getting the results back i expect.
so my only thing left is there must be a problem with my if statement and how it is dealing with the returned data as it always skips to the else even when the condition is met. The only way for the if statement to work is when i set the variable myself and run the script. The long story short is everything works fine except for the final if statement where it always jumps to the else no matter what and says the username is available even when i know its not.
Im testing on xampp. Could this be an issue too?
Any ideas? Thanks in advance!!!
Here is the code: javascript
$(document).ready(function(){
//the min chars for username
var usermin_chars = 6;
//result texts
var checking_html = 'Checking...';
//when button is clicked
$('#username').blur(function(){
var usernameVal = $("#username").val();
if (usernameVal == '') {
$("#username_result").html('<span class="error">Please enter a username!</span>');
$('#username').removeClass();
$('#username').addClass("form_error");
}
//run the character number check
else if($('#username').val().length < usermin_chars){
//if it's bellow the minimum show characters_error text '
$('#username_result').html('<span class="error">Username must contain at least 6 characters!</span>');
$('#username').removeClass();
$('#username').addClass("form_error");
}else{
//get the username
var username = $('#username').val();
$.ajax({
type:"post",
url:"checkUsername.php",
data:"username="+username,
success:function(data){
if(data==0){$("#usename_result").html("Username already in use! Please choose another username.");
$('#username').removeClass();
$('#username').addClass("form_error");
}
else{$("#username_result").html("Username available");
$('#username_result').html('<span class="ok"><img src="../images/imgs/available.png" width="20" height="20" margin-left="5" alt="tick"></span>');
$('#username').removeClass();
$('#username').addClass("form_ok");
}
}
});
}
});
});
ok so i found the answer. as the site is near completion i had gone through all the pages and made sure every page had a title for SEO including the checkUser page. the page title was being sent through with the data and confusing the if statement. when i was testing the fuction if i placed (data) in a span it only showed the 0 or 1 but looking at it in firebug it showed the response as the 0 or 1 plus the page title. Thanks for those who had a look at it.
I want to show some message in span tag after my form gets submitted to the server. The problem is, the text disappears within seconds. Is it because the page is reloaded? Can anyone spot what is wrong with my function?
function placeOrder(form) {
if (validateLength(1, 32, form["bannerMessage"], form["messageError"]) &&
validateZipField(form["zipField"], form["zipError"]) &&
validateEmptyFields(form["dateField"], form["dateError"]) &&
validateEmptyFields(form["nameField"], form["nameError"]) &&
validateEmptyFields(form["phoneField"], form["phoneError"]) &&
validateEmptyFields(form["emailField"], form["emailError"])) {
// Submit the order to the server
form.submit();
document.getElementById("submitSuccess").innerHTML = "Submitted successfully";
} else {
alert("I'm sorry but there is something wrong with the order information.");
}
}
Exactly, you are sending the form to the server who will send back a complete page.
What you should do is have the new page contain the message that the form was successfully sent, or if there was a problem with the form (you do server-side validation, right?) -- give the details of the error.
For usability, it is important to make sure that you keep all of the form-fields you can. (E-Commerce fields are special, but keep everything else.)