0

Hi i have some functions in JavaScript for a contact form, but Dreamweaver cs6 is showing syntax error. Can someone please help me? The first and second (let) syntax give an error as well as the final class user.

var fields = {};
document.addEventListener("DOMContentLoaded", function() {
fields.name = document.getElementById('name');
fields.email = document.getElementById('email');
fields.message = document.getElementById('message');
})

function isNotEmpty(value) {
if (value == null || typeof value == 'undefined' ) return false;
return (value.length > 0);
}

function isEmail(email) {
let regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
return regex.test(String(email).toLowerCase());
}

function fieldValidation(field, validationFunction) {
if (field == null) return false;

let isFieldValid = validationFunction(field.value)
if (!isFieldValid) {
field.className = 'placeholderRed';
} else {
field.className = '';
}

return isFieldValid;
} 

function isValid() {
var valid = true;

valid &= fieldValidation(fields.name, isNotEmpty);
valid &= fieldValidation(fields.email, isNotEmpty);
valid &= fieldValidation(fields.message, isNotEmpty);
valid &= fieldValidation(fields.email, isEmail);
return valid;
}

class User {
constructor(name, email, message) {
this.name = name;
this.email = email;
this.message = message;
 }
}
Ben Gali
  • 11
  • 3
  • 1
    Look at the highlighting of the post. That newline in your regex is not helping at all. – aynber Jan 29 '20 at 19:39
  • I think some character escaping might also be needed in the regex – Patrick Q Jan 29 '20 at 19:49
  • Thank you, can you help me with an example of how i could do the character escaping please? – Ben Gali Jan 29 '20 at 20:00
  • @BenGali The place I originally thought might need it actually doesn't. But the part where you do `(?:.[a-` if you expect that to match a literal `.` (dot), then you'll need to escape that like `(?:\.[a-`. Otherwise, it will match _any_ character – Patrick Q Jan 29 '20 at 20:06
  • Thanks @Patrick Q i have added that it still says syntax error on line 14. however if i replace let with var to create an instance of regex. it changes to next place i have used let. it seems this syntax is casuing error in cs6 only. i will test the code now with the changes you suggested. – Ben Gali Jan 29 '20 at 20:18
  • FYI, you might want to check out [this answer](https://stackoverflow.com/a/201378/1505169) about email address regex – Patrick Q Jan 29 '20 at 20:23
  • Yes thank you very much @Patrick Q – Ben Gali Jan 29 '20 at 20:55

0 Answers0