I'm editing Dotmailer's signup form code as some of my users won't have email addresses. I want to set the form so that if the user does not enter a value for their email address, for it to default to say test#test.com
The field must be filled for it to successfully upload for the Dotmailer DB so I can't just remove it as a required field.
Below is the script at the start of the form which seems to be causing the issues (from my limited knowledge)
<script language="javascript">// <![CDATA[
function validate_signup(frm) {
var emailAddress = frm.Email.value;
var errorString = '';
if (emailAddress == '' || emailAddress.indexOf('#') == -1) {
errorString = 'Please enter your email address';
}
var isError = false;
if (errorString.length > 0)
isError = true;
if (isError)
alert(errorString);
return !isError;
}
// ]]></script>
Ty in advance for any assistance you can provide.
You just need to change
if (emailAddress == '' || emailAddress.indexOf('#') == -1) {
errorString = 'Please enter your email address';
}
to
if (emailAddress == '' || emailAddress.indexOf('#') == -1) {
var input = document.getElementById('#myInput');
input.value = "test#test.com";
}
where #myInput is the id of the email field.
Related
I'm trying to validate an email form with jQuery, and it isn't working, here is the part of the code I think the problem is:
if (isValid == false) {
event.preventDefault();
}
I get the message that event is deprecated, what does this mean and how can I fix it?
EDIT (here's the whole code):
$("#contact_form").submit( evt => {
let isValid = true;
// validate the first name entry
const firstName = $("#first_name").val().trim();
if (firstName == "") {
$("#first_name").next().text("This field is required.");
isValid = false;
} else {
$("#first_name").next().text("");
}
$("#first_name").val(firstName);
// validate the last name entry
const lastName = $("#last_name").val().trim();
if (lastName == "") {
$("#last_name").next().text("This field is required.");
isValid = false;
} else {
$("#last_name").next().text("");
}
$("#last_name").val(lastName);
// validate the email entry with a regular expression
const emailPattern = /\b[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/;
const email = $("#email").val().trim();
if (email == "") {
$("#email").next().text("This field is required.");
isValid = false;
} else if ( !emailPattern.test(email) ) {
$("#email").next().text("Must be a valid email address.");
isValid = false;
} else {
$("#email").next().text("");
}
$("#email").val(email);
// validate the verify entry
const verify = $("#verify").val().trim();
if (verify == "") {
$("#verify").next().text("This field is required.");
isValid = false;
} else if (verify !== email) {
$("#verify").next().text("Must match first email entry.");
isValid = false;
} else {
$("#verify").next().text("");
}
$("#verify").val(verify);
// prevent the submission of the form if any entries are invalid
if (isValid == false) {
event.preventDefault();
}
}),
Change event to evt.
if (!isValid)
{
evt.preventDefault();
}
The event you're trying to prevent is the one from the 'submit' listener. Not the global event.
$("#contact_form").submit( evt => { });
Im trying to clear only the password and confirm password fields when those are not matched.
Im calling a function when the submit button is pressed which contains a password checking condition. How should I clear only password and confirmpassword inputs if that condition is true?
function checkmatch()
{
var name = $("#username").val();
var address = $("#address").val();
var email = $("#emailid").val();
var mobile = $("#phonenum").val();
var password = $("#newPwd").val();
var cnfirmPassword = $("#confirmPwd").val();
if(password != cnfirmPassword)
{
alert("Passwords do not match.");
return false;
}
else if((name==null || name == "") || (address==null || address == "") || (email==null || email == "") || (mobile=null || mobile == "") || (password==null || password == "") || (cnfirmPassword==null || cnfirmPassword == ""))
{
alert("Please fill all the required fields.");
return false;
}
else
{
$.ajax(
{
type: "POST",
url: "assmt1.php",
datatype: "html",
data: $("#fm1").serialize(),
success: function(response)
{
}
});
alert("Your form has been submitted. Thank you!");
}
}
Is this what you mean?
var password = $("#newPwd").val();
var cnfirmPassword = $("#confirmPwd").val();
if(password != cnfirmPassword)
{
alert("Passwords do not match.");
$("#newPwd").val('').focus(); ///this will not focus because of alert
$("#confirmPwd").val('');
return false;
}
All you need to do is call the same function you used to get the value.
Just pass empty string to val() like so:
$("#newPwd").val('');
$("#confirmPwd").val('');
How do I test for form validation for both variables: emailAddr && items[].
items[] is a checkbox array.
Currently the code below won't submit the form at all.
jQuery(document).ready(function(){
var re = /(\w+)\#(\w+)\.[a-zA-Z]/g;
var email = document.getElementById("emailAddr");
var emailValue = email.value;
var testEmail = re.test(emailValue);
jQuery("#submitForm").on("click",function(){
if (jQuery("input[name*='items']").is(":checked"),
testEmail === true){
return true;
} else {
jQuery('#messages').append("You must choose at least 1 image<br>
Please enter a valid email");
return false;
}
});
});
Cleaning up the code a little to check for the value on submission may help but I do not know exactly how the html is formatted to see why else the form may not be submitting.
var re = /(\w+)\#(\w+)\.[a-zA-Z]/g;
var email = document.getElementById("emailAddr");
jQuery("#submitForm").on("click",function(e){
var emailValue = email.value;
var testEmail = re.test(emailValue);
if (jQuery("input[name*='items']").is(":checked") && testEmail === true){
return true;
} else {
e.preventDefault(); // prevents the form from submitting if invalid
jQuery('#messages').append("You must choose at least 1 image<br>Please enter a valid email");
return false;
}
});
In my login script when the username and password are both correct it must go to the next page (main/menu page) of the intel XDK.
My problem is how or what code can I use to call the next page whenever the username and password is correct (login successful)?
function validateForm() {
var formUsername = document.forms.login.username.value;
var formPassword = document.forms.login.password.value;
var MINLENGTH = 5;
// Validate username and password
if (formUsername === null || formUsername === "") {
alert("Username must be filled out");
}
else if (formPassword === null || formPassword === "") {
alert("Password must be filled out");
}
else if (formUsername.length < MINLENGTH || formPassword.length < MINLENGTH) {
alert("The minimum length of username and password at least " + MINLENGTH);
}
else if(formUsername == 'admin' && formPassword == 'admin'){
alert('welcome');
//this is where should i put the code to go at the next page of the XDK API.
return;
}
alert("Login failed!!!");
}
got it guys..
it will be.. like this ..
else if(formUsername == 'admin' && formPassword == 'admin'){
alert('welcome');
activated_page("#menu");
return;
}
I am trying to create form validation using jquery. First, the code doesn't work for me and secondly I am hoping there is an easier way to use jquery to validate my form using reg ex patterns. I don't know if I am using jquery corectly and I would like to avoid using any plugins.
$(document).ready(function() {
var pattern = /^\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
var pattern1 = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
var pattern2 = /[a-zA-Z]*( [a-zA-Z]*)?/;
$('#submit').on('click', function() {
var valid = true,
errorMessage = "",
if ($('#role').val() == '') {
errorMessage = "please choose a role \n";
valid = false;
}enter code here
if ($('#state').val() == '') {
errorMessage = "please choose a state \n";
valid = false;
}
if ($('#major').val() == '') {
errorMessage = "please provide a major \n";
valid = false;
}
if ($('#degree').val() == '') {
errorMessage = "please provide a degree \n";
valid = false;
}
if ($('#location').val() == '') {
errorMessage = "please choose a location \n";
valid = false;
}
if ($('#gradyear').val() == '') {
errorMessage = "please provide a graduation year \n";
valid = false;
}
if ($('#dept').val() == '') {
errorMessage = "please provide your department \n";
valid = false;
}
if ($('#campadd').val() == '') {
errorMessage += "please enter your address\n";
valid = false;
}
if($('#email').val() !== ""){
if(!pattern.test('#email'.val())){
errorMessage += "email invalid!\n";
valid = false;
}
if($('#telephone').val() !== ""){
if(!pattern1.test('#telephone'.val())){
errorMessage += "telephone number invalid!\n";
valid = false;
}
}}
if( !valid && errorMessage.length > 0){
alert(errorMessage);
}
First, the code formatting in your post is a bit off, which makes it somewhat difficult to read. What exactly do you mean when you say it "doesn't work"?
My best guess is that you mean the form still submits, even after the errorMessage alert box appears. If this is the case, that is probably because you are not canceling the submit event.
Change your code like this:
// submit instead of click event, give a name to the event argument
$('#submit').on('submit', function(e) {
//...
if( !valid && errorMessage.length > 0){
e.preventDefault(); // prevents the form submission
alert(errorMessage);
}