JavaScript validate various form elements - javascript

I have a script thats trying to validate some form elements but i cant figure out the correct syntax for each "if" I need, its doing absolutely nothing. I believe the issue may be that on the same form there are many "tabs" or other forms so I may have to write which form the element belongs to but I dont know the syntax for that, like if its
var NewStatus = document.getElementById("ChangeStatusForm.CURRENT_STATUS").VALUE;
if none of the alerts apply then i just need it to submit the form
<script type="text/javascript">
function ValidateInfo()
var NewStatus = document.getElementById("CURRENT_STATUS").VALUE;
var OldStatus = document.getElementByID("ovpInfo.current_status").VALUE;
var CheckNumber = document.getElementByID("check_number").VALUE;
var CheckDate = document.getElementByID("check_date").VALUE;
if (NewStatus == 0){
alert ("Please select a New Status");
}
else if (NewStatus == 5){
alert ("This claim has already been collected");
} else if (NewStatus == 10){
alert ("This claim has already been collected");
} else if (NewStatus == 10 and CheckNumber =="" or CheckDate == ""){
alert ("Please enter Payment Details");
else
document.forms['ChangeStatusForm'].submit() ;
}
</script>
In order to just get at least one variable check working ive edited my script. this does not work either though (used some syntax I found to specify the actual form the element belongs to)
<script type="text/javascript">
function ValidateInfo()
{
var NewStatus = document.forms["ChangeStatusForm"]["CURRENT_STATUS"].value;
if (NewStatus == 0){
alert ("Please select a New Status");
}
}
</script>

you are using a wrong expression for "document.getElementById("id")" and you need to change the or, and for "&&", "||".
And I using a flag for return the correct value.
for example:
function ValidateInfo()
var flag = true;
var NewStatus = document.getElementById("CURRENT_STATUS").value;
var OldStatus = document.getElementByID("ovpInfo.current_status").value;
var CheckNumber = document.getElementByID("check_number").value;
var CheckDate = document.getElementByID("check_date").value;
if (NewStatus == 0){
alert ("Please select a New Status");
flag=false;
}
else if (NewStatus == 5){
alert ("This claim has already been collected");
flag=false;
} else if (NewStatus == 10){
alert ("This claim has already been collected");
flag=false;
} else if (NewStatus == 10 and CheckNumber =="" or CheckDate == ""){
alert ("Please enter Payment Details");
flag=false;
}else{
flag=true;
}
if( flag ){document.forms['ChangeStatusForm'].submit()}
good luck

Related

JavaScript Multiple Entry Form Validation

I am trying to use Javascript to validate user input before page redirects.
Users can either Input Student ID, Name, or Gender, and based on their input, they will be redirected to a URL.
However, I don't seem to get the multiple entries correctly in my javascript and nothing happened when the submit button is clicked.
I have tried different solutions which I found here.
see my JavaScript code below;
var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("studentid").value;
if ( studentid == "12345" || studentid == "Daniel" || studentid == "Boy"){
alert ("Correct Input");
window.location = "https://www.google.com";
// Redirecting to other page.
return false;
}
else{
attempt --;// Decrementing by one.
alert("ATTENTION!\nInvalid student ID!\nNot associated with any student\nYou have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("studentid").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}
I have tried to use the solutions below;
if ( studentid == "#12345, #Daniel, #Boy"));{
alert ("correct input");
window.location = "https://www.google.com";
// Redirecting to other page.
if ( studentid == '12345', 'Daniel', 'Boy'){
alert ("correct input");
window.location = "https://www.amazon.com";
// Redirecting to other page.
After so many attempts, I finally got it right!
var attempt = 3;
function check(form)
{
if(form.studentid.value == "12345" || form.studentid.value == "DANIEL" || form.studentid.value == "BOY")
{
window.location.replace('https://www.google.com')
return false;
}
else
{
attempt --;// Decrementing by one.
alert("ATTENTION!\nInvalid student ID!\nNot associated with any student!\nYou have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("studentid").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}

Javascript text alert

How can I use JavaScript to recognize if a text box is filled, and if not, to alert me that it must be filled.
I have tried using
function myFunction() {
alert
You can try with the following fiddle, but its better if you google your query because it is just a basic query:
$("button").click(function(){
var inputVal = $("input[name=input]").val();
if(inputVal == "" || inputVal == null){
alert("Please fill the box");
}else{
alert("Good to go");
}
});
https://jsfiddle.net/thisisdg/s7bovqvw/

javascript form validation does not work and need to fix

I am doing class homework about form validation and write some codes but they are not running at all. Can someone check it out what's wrong in them and fix?The first two functions are working properly. The problem is the third function formCheck for form validation does not work. If I put them in the same js document, the third function would also cause the other 2 functions to stop working. Thanks
and this is the corresponding webpage working with the javascript: http://www.acsu.buffalo.edu/~mintingt/jsvalidation.html
window.onload = function() {
document.getElementById("time").textContent = new Date();
}
function changeColor(value)
{
var color = document.body.style.backgroundColor;
switch(value)
{
case 'lightpink':
color = "#FFB6C1";
break;
case 'white':
color = "#FFFFFF";
break;
case 'plum':
color = "#DDA0DD";
break;
}
document.body.style.backgroundColor = color;
}
function formCheck()
{
var form = document.getElementById("myform");
if(form["first name"]== "") {
alert("Please fill in the required first name.");
form["first name"].focus();
return false;
}
if(form["last name"]== "") {
alert("Please fill in the required last name.");
form["last name"].focus();
return false;
}
var zip = /^\d{5}(-\d{4})?$/;
if(zip.test(form["zip code"]).value == false){
alert("Please fill in a valid zipcode.");
form["zip code"].focus();
return false;
}
var phone = /^\[0-9]{10}$|^\([0-9]{3}\)[ ]?[0-9]{3}-[0-9]{4}$/;
if(phone.test(form["phone number"]).value == false){
alert("Phone number input format is not valid.");
form["phone number"].focus();
return false;
}
var email = /^([a-zA-Z0-9_.])+#[a-zA-Z0-9]([a-zA-Z0-9])+\.([a-z])+$/;
if(email.test(form["email address"].value == false){
alert("Email format is not valid.");
form["email address"].focus();
return false;
}
form.submit();
return true;
}
You missed a parentheses ) in:
if(email.test(form["email address"].value == false){
This is the error dump:
Timestamp: 30/04/14 18:20:25
Error: SyntaxError: missing ) after condition
Source File: http://www.acsu.buffalo.edu/~mintingt/validation2.js
Line: 32, Column: 53
Source Code:
if(email.test(form["email address"].value == false){

JavaScript redirection error

I am trying to use JavaScript to redirect to a PHP page on my site, however, when I do it, nothing happens, apart from the alert boxes if I do not fill in the parameters. Is there something I am doing wrong?
document.getElementById("submit").onclick = check;
function check()
{
if(document.getElementById("name").value == "")
alert("The field 'Name' is required.");
else if (document.getElementById("message").value == "")
alert("The field 'Message' is required");
else
window.location.href = "scripts/main/contact.php?msg=" + document.getElementById("message").value;
}
Your default form action takes place overriding your redirect. Return false from the handler to prevent it from taking place:
function check()
{
if(document.getElementById("name").value == "")
alert("The field 'Name' is required.");
else if (document.getElementById("message").value == "")
alert("The field 'Message' is required");
else
window.location.href = "scripts/main/contact.php?msg=" + document.getElementById("message").value;
return false; // <------ here
}

Javascript validation to open javascript alert on success?

First off, I don't know much about JS so please be patient with me.
I found this free validation JS and implemented it into my website, however, after a successful validation it doesn't tell you the form has been submitted.
My question is, is it possible to make this validation summon Apprise.js alert on a successful validation? Apprise
$(document).ready(function(){
//global vars
var form = $("#ContactForm");
var name = $("#name");
var nameInfo = $("#nameInfo");
var email = $("#email");
var emailInfo = $("#emailInfo");
var subject = $("#subject");
var subjectInfo = $("#subjectInfo");
var message = $("#message");
//On blur
name.blur(validateName);
email.blur(validateEmail);
subject.blur(validateSubject);
//On key press
name.keyup(validateName);
subject.keyup(validateSubject);
message.keyup(validateMessage);
//On Submitting
form.submit(function(){
if(validateName() & validateEmail() & validateSubject() & validateMessage())
return true
else
return false;
});
//validation functions
function validateEmail(){
//testing regular expression
var a = $("#email").val();
var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+#[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
//if it's valid email
if(filter.test(a)){
email.removeClass("error");
emailInfo.text("");
emailInfo.removeClass("error");
return true;
}
//if it's NOT valid
else{
email.addClass("error");
emailInfo.text("Please type a valid e-mail address!");
emailInfo.addClass("error");
return false;
}
}
function validateName(){
//if it's NOT valid
if(name.val().length < 4){
name.addClass("error");
nameInfo.text("Name must be atleast 4 letters (include last name)!");
nameInfo.addClass("error");
return false;
}
//if it's valid
else{
name.removeClass("error");
nameInfo.text("");
nameInfo.removeClass("error");
return true;
}
}
function validateSubject(){
//if it's NOT valid
if(subject.val().length < 4){
subject.addClass("error");
subjectInfo.text("Subject must be atleast 4 characters!");
subjectInfo.addClass("error");
return false;
}
//if it's valid
else{
subject.removeClass("error");
subjectInfo.text("");
subjectInfo.removeClass("error");
return true;
}
}
function validateMessage(){
//it's NOT valid
if(message.val().length < 1){
message.addClass("error");
return false;
}
//it's valid
else{
message.removeClass("error");
return true;
}
}
});
You can add a prompt dialog box alert('Your Email has been sent.'); before return true, in form.submit function.

Categories

Resources