Javascript validation not working on submit - javascript

I programmed this to check my form data, however, it does not work. First I check all the values, if they are null, then I check if email adress is valid and then I check if the checkbox is checked.
When I submit the form with no values on input, it successfully loads the document specified in action="#", which should not happen because of the return false;.
function validateForm() {
var meno = document.forms["registracia"]["meno"].value;
var priezvisko = document.forms["registracia"]["priezvisko"].value;
var telefon = document.forms["registracia"]["telefon"].value;
var email = document.forms["registracia"]["email"].value;
var vek = document.forms["registracia"]["vek"].value;
var praca = document.forms["registracia"]["praca"].value;
var motto = document.forms["registracia"]["motto"].value;
var osoba = document.forms["registracia"]["osoba"].value;
if (meno == null || meno =="" || priezvisko == null || priezvisko =="" ||
telefon == null || telefon =="" || email == null || email =="" ||
vek == null || vek =="" || praca == null || praca =="" ||
motto == null || motto =="" || osoba == null || osoba =="" ||) {
alert("Musíte vyplniť všetky údaje.");
return false;
}
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!re.test(email)) {
alert("Zadajte platnú e-mailovú adresu.");
return false;
}
if(!document.forms["registracia"]["suhlas"].checked) {
alert("Musíte súhlasiť s uverejnením vašej fotografie.");
return false;
}
}

You had an extra || at the end of your if condition. You can also make it a bit more readable, see below.
function validateForm() {
var form = document.forms["registracia"];
if ( !form.meno.value
|| !form.priezvisko.value
|| !form.telefon.value
|| !form.email.value
|| !form.vek.value
|| !form.praca.value
|| !form.motto.value
|| !form.osoba.value ) {
alert("Musíte vyplniť všetky údaje.");
return false;
}
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!re.test(form.email.value)) {
alert("Zadajte platnú e-mailovú adresu.");
return false;
}
if( !form.suhlas.checked ) {
alert("Musíte súhlasiť s uverejnením vašej fotografie.");
return false;
}
}

Related

Check to see if form fields are empty using javascript

I currently have a working function that checks if all of the forms fields are not blank. For part of a form, i have a hidden div that will appear if one of the fields has an answer of yes selected in the drop down. when i have the drop down item selected as yes, i would like for the function to check if any one of those fields are left empty, and when the drop down has an answer of no, i don't want the function to check the values of the hidden div items. i can't seem to figure out a way to accomplish this. this is what i have tried so far. the variable label2 contains the value of the drop down menu, Yes/No.
function validateOverage()
{
var drive2 = document.forms["overage"]["drivers2"].value;
var prodes2 = document.forms["overage"]["proddes2"].value;
var sizes2 = document.forms["overage"]["size2"].value;
var cases2 = document.forms["overage"]["cs2"].value;
var bottle2 = document.forms["overage"]["btls2"].value;
var initials2 = document.forms["overage"]["chkitls2"].value;
var label2 = document.forms["overage"]["ontrucks"].value;
var routenum2 = document.forms["overage"]["routenum"].value;
var stopnum2 = document.forms["overage"]["stopnum"].value;
var custnum2 = document.forms["overage"]["custnum"].value;
if (drive2 == null || drive2 == "" || prodes2 == null || prodes2 == "" || sizes2 == null || sizes2 == "" || cases2 == null || cases2 == ""||
bottle2 == null || bottle2 == "" || initials2 == null || initials2 == "" || label2 == null || label2 == "")
{
alert("Fill in all Fields for the Overage form");
return false;
}
else if (label2 == "Yes" || routenum2 == null || routenum2 == "" || stopnum2 == null || stopnum2 == "" || custnum2 == null || custnum2 == "")
{
alert("Fille in all Fields for the Overage form");
return false;
}*/
else if (cases2 == "0" && bottle2 == "0")
{
alert("Both cases and bottles values can not be 0");
return false;
}
else
{
alert("Data Added to the Overage form");
}
}
html code that i have in my body that has the hidden contents
<td style = "visibility: hidden"><input type = "number" id = "routenum"> </input></td>
<td style = "visibility: hidden"><input type = "number" id = "stopnum"> </input></td>
<td style = "visibility: hidden"><input type = "number" id = "custnum"> </input></td>

Javascript alert on dropdown menus?

I have a javascript alert on my form here - http://investing.uglyopportunities.com/opportunity/ I have the javascript function working correctly for my form. Except I also want the javascript to alert when the user doesn't select from either of the two drop down menus. I really don't have much experience at all with javascript, so keep that in mind! Here is my current code. I really appreciate any help
<script type="text/javascript">// <![CDATA[
function validateForm() {
var a = document.forms["myform"]["inf_field_FirstName"].value;
var b = document.forms["myform"]["inf_field_Email"].value;
var c = document.forms["myform"]["inf_field_Phone1"].value;
var e = document.forms["myform"]["inf_field_City"].value;
var f = document.forms["myform"]["inf_field_State"].value;
var g = document.forms["myform"]["inf_field_PostalCode"].value;
if (a == null || a == "" || a == "First Name Here") {
alert("Please enter your First Name!");
return false;
}
if (c == null || c == '' || c == "Enter Your Phone Here" || c.length < 9) {
alert("Please insert your phone number!");
return false;
}
if (e == null || e == '' ||e == "City") {
alert("Please insert your city");
return false;
}
if (f == null || f == '' || f == "State") {
alert("Please insert your state ");
return false;
}
if (g == null || g == '' ||g == "Postal Code" || c.length < 5) {
alert("Please insert your postal code");
return false;
}
var emailRegEx = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.myform.inf_field_Email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
return false;
}
}
// ]]>
</script>
if(document.getElementById('inf_custom_LevelofInterest').value == '' || document.getElementById('inf_custom_Doyouhavemoneytoinvest').value == ''){
alert('please select ...');
return false;
}

Why is my form not validating on submit? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a sign up form on my page at http://business.uglyopportunities.com/affiliate-signup/ (scroll down to see the form)
Keep in mind, I am not very good with JS so this may be a simple error.
Anyways, here is the first line of my form telling it to validate:
<form accept-charset="UTF-8" action="xxxxx" class="infusion-form" method="POST" name="myform" onsubmit="return validateForm();">
and here is my validateForm javascript:
<script type="text/javascript">
function validateForm() {
var a = document.forms["myform"]["inf_field_FirstName"].value;
var b = document.forms["myform"]["inf_field_Email"].value;
var c = document.forms["myform"]["inf_field_Phone1"].value;
var d = document.forms["myform"][" inf_field_StreetAddress1"].value;
var e = document.forms["myform"][" inf_field_City"].value;
var f = document.forms["myform"][" inf_field_State"].value;
var g = document.forms["myform"][" inf_field_PostalCode"].value;
var h = document.forms["myform"][" inf_other_Username"].value;
var i = document.forms["myform"][" inf_other_Password"].value;
var j = document.forms["myform"][" inf_other_RetypePassword"].value;
if (a == null || a == "" || a == "First Name Here") {
alert("Please enter your First Name!");
return false;
}
if (c == null || c == '' || c == "Enter Your Phone Here" || c.length < 9) {
alert("Please insert your phone number!");
return false;
}
if (d == null || d == '' || d == "Street Address”) {
alert("Please insert your street address ");
return false;
}
if (e == null || e == '' ||e == "City”) {
alert("Please insert your city");
return false;
}
if (f == null || f == '' || f == "State”) {
alert("Please insert your state ");
return false;
}
if (g == null || g == '' ||g == "Postal Code”) {
alert("Please insert your postal code");
return false;
}
if (h == null || h == '' || h == "Username”) {
alert("Please insert your username ");
return false;
}
if (i == null || i == '' ||i == "password”) {
alert("Please insert your password");
return false;
}
if (j == null || j == '' || j == "password”) {
alert("Please re - type your password ! ");
return false;
}
var emailRegEx = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.myform.inf_field_Email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
return false;
}
}
</script>
and that is not working. However, when I used this code, it worked fine:
<script type="text/javascript">
function validateForm() {
var a=document.forms["myform"]["inf_field_FirstName"].value;
var b=document.forms["myform"]["inf_field_Email"].value;
var c=document.forms["myform"]["inf_field_Phone1"].value;
if (a==null || a=="" || a=="First Name Here") {
alert("Please enter your First Name!");
return false;
}
if (c==null || c==''|| c=="Enter Your Phone Here" || c.length < 9) {
alert("Please insert your phone number!");
return false;
}
var emailRegEx = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.myform.inf_field_Email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
return false;
}
}
</script>
UPDATED CODE *
<script type="text/javascript">// <![CDATA[
function validateForm() {
var a = document.forms["myform"]["inf_field_FirstName"].value;
var b = document.forms["myform"]["inf_field_Email"].value;
var c = document.forms["myform"]["inf_field_Phone1"].value;
var d = document.forms["myform"]["inf_field_StreetAddress1"].value;
var e = document.forms["myform"]["inf_field_City"].value;
var f = document.forms["myform"]["inf_field_State"].value;
var g = document.forms["myform"]["inf_field_PostalCode"].value;
var h = document.forms["myform"]["inf_other_Username"].value;
var i = document.forms["myform"]["inf_other_Password"].value;
var j = document.forms["myform"]["inf_other_RetypePassword"].value;
if (a == null || a == "" || a == "First Name Here") {
alert("Please enter your First Name!");
return false;
}
if (c == null || c == '' || c == "Enter Your Phone Here" || c.length < 9) {
alert("Please insert your phone number!");
return false;
}
if (d == null || d == '' || d == "Street Address") {
alert("Please insert your street address ");
return false;
}
if (e == null || e == '' ||e == "City") {
alert("Please insert your city");
return false;
}
if (f == null || f == '' || f == "State") {
alert("Please insert your state ");
return false;
}
if (g == null || g == '' ||g == "Postal Code") {
alert("Please insert your postal code");
return false;
}
if (h == null || h == '' || h == "Username") {
alert("Please insert your username ");
return false;
}
if (i == null || i == '' ||i == "password") {
alert("Please insert your password");
return false;
}
if (j == null || j == '' || j == "password") {
alert("Please re - type your password ! ");
return false;
}
var emailRegEx = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (document.myform.inf_field_Email.value.search(emailRegEx) == -1) {
alert("Please enter a valid email address.");
return false;
}
}
// ]]>
</script>
You have a white space here:
var d = document.forms["myform"][" inf_field_StreetAddress1"].value;
// ^-----------------------
You have the same problem with for e,f,g,h,i,j
Also, you used the wrong quotes sign:
"Street Address”
// ^--------------
You did it several times.
As gdoron says, it's probably an spelling problem. You cannot begin the name of an element with a whitespace http://www.w3.org/TR/html401/types.html#type-name

Java Script: onsubmit="check_rfields();" in form tag of html. But getting failed in validation

onsubmit="check_rfields();" in form tag of html. But getting failed in validation.
function check_rfields() {
var name = document.getElementById("id_rname").value;
var email = document.getElementById("id_remail").value;
var cemail = document.getElementById("id_rcemail").value;
var password = document.getElementById("id_rpassword").value;
var cpassword = document.getElementById("id_rcpassword").value;
if(name=='' || name==0
&& email=='' || email==0
&& cemail=='' || cemail ==0
&& password=='' || password==0
&& cpassword=='' || cpassword==0 ) {
document.getElementById("onsubmitErr").innerHTML
= "All fields arerequired";
return false;
}
return true;
}
If you want the submit to fail (on failed validation), use:
onSubmit="return check_rfields()"

Refresh page on Cancel prompt

Problem:
I have a web page. When I fill in the data and hit save, it gives me a a confirmation pop up box. On cancel I want to refresh the page. So the data entered is cleared.
Here is the code for confirmation so far:
function checknumber() {
var x = document.getElementById('<%=view_txt_pixelwidth.ClientID%>').value;
var y = document.getElementById('<%=view_txt_pixelheight.ClientID%>').value;
var z = document.getElementById('<%=view_txt_printleft.ClientID%>').value;
var a = document.getElementById('<%=view_txt_printtop.ClientID%>').value;
var b = document.getElementById('<%=view_txt_printwidth.ClientID%>').value;
var c = document.getElementById('<%=view_txt_printheight.ClientID%>').value;
var anum = /(^\d+$)|(^\d+\.\d+$)/
if ((anum.test(x)) && (anum.test(y)) && (anum.test(z)) && (anum.test(a)) && (anum.test(b)) && (anum.test(c)))
testresult = confirm('save change?');
else {
alert("Please input a valid number!")
testresult = false
}
return (testresult);
}
function Validate_view() {
var value = document.getElementById('<%=view_txt_name.ClientID%>').value;
var value2 = document.getElementById('<%=view_txt_title.ClientID%>').value;
var value3 = document.getElementById('<%=view_txt_description.ClientID%>').value;
var value4 = document.getElementById('<%=view_txt_pixelwidth.ClientID%>').value;
var value5 = document.getElementById('<%=view_txt_pixelheight.ClientID%>').value;
if (value == '' || value2 == '' || value3 == '' || value4 == '' || value5 == '') {
ValidatorEnable(document.getElementById('<%= view_req_pixelwidth.ClientID %>'), true);
ValidatorEnable(document.getElementById('<%= view_req_pixelheight.ClientID %>'), true);
ValidatorEnable(document.getElementById('<%= view_req_name.ClientID %>'), true);
ValidatorEnable(document.getElementById('<%= view_req_title.ClientID %>'), true);
ValidatorEnable(document.getElementById('<%= view_req_description.ClientID %>'), true);
}
if (value == '' || value2 == '' || value3 == '' || value4 == '' || value5 == '') {
alert("Please enter the missing fields");
return false;
}
else if (document.layers || document.all || document.getElementById) {
return checknumber();
}
<asp:Button ID="view_btn_save" Text="Save" OnClick="view_btn_save_click"
OnClientClick="return Validate_view(); "
}
if ((anum.test(x)) && (anum.test(y)) && (anum.test(z)) && (anum.test(a)) && (anum.test(b)) && (anum.test(c)))
testresult = confirm('save change?');
if(testresult == 0)
{
//cancel
//window.location.reload(true);
window.location.href=window.location.href;
}
else {
alert("Please input a valid number!")
testresult = false
}

Categories