I have the following script that works fine until I add the other javascript below...
First Script in the header tags
function validateForm() {
var valid = true;
var errMsg = "";
var email = document.emailform.email.value;
var filter = /^([a-zA-Z0-9_.-])+#(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if(email.length <= 0) {
valid = false;
errMsg += "Email address is required.\n";
} else {
if (!filter.test(email)) {
valid = false;
errMsg += "Please provide a valid email address.\n";
}
}
if (errMsg.length > 0) {
alert(errMsg);
return false;
}
}
and just before the closing tags I have...
$('#form').submit(validateForm);
The above works fine, except once I add the script below, validateForm no longer works. This following is added just before the closing body tags.
cbr202=Math.random()*10000000000000000;document.write('<scr'+'ipt language="JavaScript" src="http://example.com/landing.php?lpip=411&202cb='+cbr202+'" type="text/javascript"></scr' + 'ipt>');
I can't seem to figure out what's causing the issue. Hoping someone with experience can see the problem.
Solved: I figured it out... it was due to my own sloppiness. I should have the jquery event handler below the document.write script, not above it.
You forgot to add a closing } to the function. That caused an error and resulted in any JS coming after that to not execute.
function validateForm() {
var valid = true;
var errMsg = "";
var email = document.emailform.email.value;
var filter = /^([a-zA-Z0-9_.-])+#(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if(email.length <= 0) {
valid = false;
errMsg += "Email address is required.\n";
} else {
if (!filter.test(email)) {
valid = false;
errMsg += "Please provide a valid email address.\n";
}
}
if (errMsg.length > 0) {
alert(errMsg);
return false;
}
}
the url generated by
src="http://mysite.com/landing.php?lpip=411&202cb='+cbr202
does not exist. The browser tries to load the script from the url using a get request and fails.
cbr202=Math.random()*10000000000000000;
I think you need change code same below
var cbr202=Math.random()*10000000000000000;
Related
I am doing a login for a page, if I enter index.html it has to redirect me to the login, html.
the issue here is that I am getting an infinite loop and I don't know what my syntax error is, I would appreciate your help!
This is where the error is:
sessionStorage.setItem('usuario',false)
var usuario = sessionStorage.getItem(usuario)
var logueate = window.location.replace('login.html')
if (!sessionStorage.getItem('usuario')) {
logueate;}
my login JS code
function validacion(){
var email = document.getElementById("email").value;
var password = document.getElementById("password").value;
if (email == "" ) {
document.getElementById("errores").innerHTML = "Campos invalidos";
} else if (password == "") {
document.getElementById("errores").innerHTML = "Campos invalidos";
}
else { window.location.href="index.html"
Does your login.html is alos executing this code, if yes then it is redirecting again and again to login.html page.
I am trying to work on verifying OTP. Here I have two components that are:
Textbox which takes input of OTP. id="txtOTP"
An Status Line (here i have used <i> tag) that shows status of verified OTP. id="statusLine"
I am using JavaScript for this purpose.
function checkOTP()
{
var OTP = "1234";
var txtOTP = document.getElementById('txtOTP');
var statusLine = document.getElementById('statusLine');
var myOTP = txtOTP.value;
if (OTP.value == myOTP)
{
console.log('Entered in Valid OTP');
statusLine.style.display = "inline";
statusLine.style.color = "green";
statusLine.innerHTML = "OTP Verified, Generating Your Pass and Redirecting to the Next Page... ";
console.log('Exit From Valid OTP');
return true;
}
else if (OTP.value != myOTP)
{
console.log('Entered in Invalid OTP');
statusLine.style.display = "inline";
statusLine.style.color = "red";
statusLine.innerHTML = "Invalid OTP. Please Try Again";
console.log('Exit From Invalid OTP');
return false;
}
}
As Per my code it should go to the if's scope if OTP is correct, and it should go to the else's scope if OTP is wrong.
However, it always goes to the else's scope even though I am writing the correct OTP in the textbox. I have even tried this code without using if with the else statement (like else if() { } ).
You need to either change myOTP to a number or use double equals:
var myOTP = parseInt(txtOTP.value);
Or:
if (OTP == myOTP) {...}
Also note that you don't need else if (...) - just use else {...}.
OTP is a Number but you check OTP.value in if/else if statements
function checkOTP()
{
var OTP = 1234;
var txtOTP = document.getElementById('txtOTP');
var statusLine = document.getElementById('statusLine');
var myOTP = txtOTP.value;
if(OTP === myOTP )
{
console.log('Entered in Valid OTP');
statusLine.style.display = "inline";
statusLine.style.color = "green";
statusLine.innerHTML = "OTP Verified, Generating Your Pass and Redirecting to the Next Page... ";
console.log('Exit From Valid OTP');
return true;
}
else if(OTP != myOTP )
{
console.log('Entered in Invalid OTP');
statusLine.style.display = "inline";
statusLine.style.color = "red";
statusLine.innerHTML = "Invalid OTP. Please Try Again";
console.log('Exit From Invalid OTP');
return false;
}
}
Here is a solution. Its based on the comments and previous answers:
function checkOTP() {
var OTP = "1234";
var txtOTP = document.getElementById('txtOTP');
var statusLine = document.getElementById('statusLine');
var myOTP = txtOTP.value;
if (OTP == myOTP) {
console.log('Entered in Valid OTP');
statusLine.style.display = "inline";
statusLine.style.color = "green";
statusLine.innerHTML = "OTP Verified, Generating Your Pass and Redirecting to the Next Page... ";
console.log('Exit From Valid OTP');
return true;
} else {
console.log('Entered in Invalid OTP');
statusLine.style.display = "inline";
statusLine.style.color = "red";
statusLine.innerHTML = "Invalid OTP. Please Try Again";
console.log('Exit From Invalid OTP');
return false;
}
}
You needed to write OTP instead of OTP.value and you don't need and else if for the opposite. Just else will do.
try adding a else statement after the else if since the syntax is :
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
When the browser loads a js file, it's working with document.getElementById("name").
But when I change to jQuery style, which is $("#name"), that particular element doesn't seem to work any more.
This is how I write my script file in the HTML (right above the closing body tag)
<script src="http://code.jquery.com/jquery-latest.min.js"> </script>
<script src="js/form.js"></script>
</body>
Does anyone knows why jQuery doesn't work?
Edit:
It's just simple code, replacing all document.getElementById to $.
But since you asked, this is the original js code that I've used:
function formValidation() {
var name = document.getElementById("name"),
email = document.getElementById("email"),
phone = document.getElementById("phone"),
message = document.getElementById("message"),
nameRe = /[a-zA-Z]/,
emailRe = /^[a-zA-Z0-9._]+#[a-zA-Z0-9.]+\.[a-zA-Z]{2,4}$/,
phoneRe = /[0-9]/,
messageError = "";
document.getElementById("frmContact").onsubmit = function () {
messageError = "";
// Validation for name, email and phone, using regular expression
if (!nameRe.test(name.value)) {
messageError = errorHighlight(name, "Invalid name");
} else if (!emailRe.test(email.value)) {
messageError = errorHighlight(email, "Invalid email");
} else if (!phoneRe.test(phone.value)) {
messageError = errorHighlight(phone, "Invalid phone");
} else if (message.value.length <= 50) {
messageError = errorHighlight(message, "Message must be at least 50 characters");
}
// form validation
if (messageError !== "") {
document.getElementById("errorMessage").innerHTML = messageError;
return false;
}
return true;
};
}
use .val() ex: name.val() or $("#name").val()
if (!nameRe.test(name.val())) {
messageError = errorHighlight(name, "Invalid name");
} else if (!emailRe.test(email.val())) {
messageError = errorHighlight(email, "Invalid email");
} else if (!phoneRe.test(phone.val())) {
messageError = errorHighlight(phone, "Invalid phone");
} else if (message.val().length <= 50) {
messageError = errorHighlight(message, "Message must be at least 50 characters");
}
Following how to use it as a small example:
<!DOCTYPE html>
<html lang="de">
<head>
<title>Welcome</title>
</head>
<body>
<h1>It Works!</h1>
<div id="changethat"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"> </script>
<script>
(function($,undefined){
$('#changethat').html('Changed!');
})(jQuery);
</script>
</body>
</html>
JSFiddle: http://jsfiddle.net/qxamct1n/1/
I am trying to validate 3 fields in a html form using javascript. If any of the fields are empty an alert box appears with a message indicating which fields are empty. This all works fine. I'm having a problem getting the message (msg2) to appear in an alert box when the form has been completed properly. My code is below-I know it's just something simple that I'm missing if anyone can help. Thanks!
var valid = true;
var msg="Incomplete form:\n";
var msg2="Success! There are no null fields.";
if ( myname== "" ) {
msg+="You need to fill the name field!\n";
valid = false;
}
if ( emailaddress == "" ) {
msg+="You need to fill in your email!\n";
valid = false;
}
if ( commentString == "" ) {
msg+="You need to fill in your comment!\n";
valid = false;
}
if ((!myname=="")&&(!emailaddress=="")&&(!commentString=="")){
return msg2;
}
if (!valid) alert(msg);
return valid;
}
You're right, it's something simple: return msg2; will not open an alert box. You still have to call alert() somewhere.
i think a little change in your code will solve the problem:
if (!valid) alert(msg);
return valid;
}
change it to
if (!valid) {
alert(msg);
return valid;
}
if (!valid) {
alert(msg);
} else {
alert(msg2);
}
return valid
Maybe somethign like that? And remove the block with if ((!myname=="")&& ...
Here is the code I have now.
function ns_check()
{
var login = document.forms['vpnForm'].login.value;
var passwd = document.forms['vpnForm'].passwd.value;
var domainname = login.indexOf("\\")
if (domainname = - 1){
window.alert(_("You need to enter a domain name")); return false;
}
if (login == ""){
window.alert(_("You need to enter login name")); return false;
}
if (passwd == ""){
window.alert(_("You need to enter passwd")); return false;
}
return true;
}
When you do not use a backslash in the login box it does popup an alert window, but it is empty.
Any help would be appreciated.
if (domainname = - 1)
should be
if (domainname === - 1)