0

I want to validate 2 html forms - First form is on the FAQ page and the second form is on the contact us page. Both forms have name and phone as common input fields so I want to validate both form conveniently in JS by using Class.

My JS code is as follows for validating Name and Phone input field for FAQ form.

class FormValidate {
    constructor(nameField, phoneField,  emailField, form) {
        this.nameField = nameField;     // name input field
        this.phoneField = phoneField;   // phone input field
        this.emailField = emailField;   // email input field
        this.form = form;
    }

    // method for validation of name input
    validateName(nameField) {
        const regName = new RegExp("^[a-zA-Z\\s]*$");
        let isNameValid = false;
        let name_z = nameField.value.trim(); // input value of Name input field
        let isNameHasValidLength = name_z.length < 3 || name_z.length > 20 ? false : true;

        // Name input field is not empty and contain proper data -> Not Empty && value must be between 3 to 20 characters && follow reg expression for validation
        if( !(name_z === '') && isNameHasValidLength && (regName.test(name_Z)) ){
            isNameValid = true;
        }
        return isNameValid;
    }

    validatePhone(phoneField) {
        let isPhoneValid = false;
        let phone_z = phoneField.value.trim(); // input value of Phone input field
        let isPhoneHasValidLength = phone_z.length < 10 || phone_z.length > 13 ? false : true;  // making sure that phone number is between 10 to 13 digits -> +91 and rest 10 digits

        const regPhone = new RegExp("^([0|+[0-9]{1,5})?([7-9][0-9]{9})$");

        // Validating Phone Number -> Not Empty && Must have 10 to 13 digits only && follow reg expression for validation
        if( !(phone_z === '') && isPhoneHasValidLength && regPhone.test(phone)) {
            isPhoneValid = true;
        }
        return isPhoneValid;
    }
}

let faqForm = new FormValidate(document.querySelector('#name'), document.querySelector('#phone'), null, document.querySelector('#faq-form'));

faqForm.form.addEventListener('submit', function(e) {
    let nameOkay;
    let phoneOkay;
    let submitOkay;

    nameOkay = faqForm.validateName(faqForm.nameField);
    phoneOkay = faqForm.validatePhone(faqForm.phoneField);
    submitOkay = nameOkay && phoneOkay;

    // Prevent form submission if form input is not okay
    if (!submitOkay) {
        e.preventDefault();
    }
});

HTML form code -

<form id="faq-form" action="mail-faq.php" method="POST">
              <div class="faq-form-group"> <span class="faq-form-span-text">Name</span>
                <input type="text" placeholder="i.e, John Smith" name="name" id="name" autocomplete="off" required>
              </div>
              <div class="faq-form-group"><span class="faq-form-span-text">Phone</span>
                <input type="text" placeholder="+91 123456789" name="phone" id="phone" autocomplete="off" required>
              </div>
              <div class="faq-form-group"><span class="faq-form-span-text">Message</span>
                <textarea placeholder="Your question" name="message" id="message" autocomplete="off" required></textarea>
              </div>
              <div class="faq-form-group">
                <button class="faq-form-submit-btn" id="submit" type="submit" name="submit">Submit </button>
              </div>
</form>

The problem is that nameField.value.trim(); and phoneField.value.trim(); statements are returning value - "" Which makes the validation false.

How can I fix this problem?

1
  • What does just logging nameField.value return? Commented Mar 31, 2021 at 18:27

1 Answer 1

1

When you correct the typo here in (regName.test(name_Z)) (lowercase z) then the trimed vars are as expected (not "")...

Sign up to request clarification or add additional context in comments.

2 Comments

It worked after fixing that typo. And also there is another typo in regPhone.test(phone) . It should be regPhone.test(phone_z) Thanks @biberman
when it helped you than mark the answer as exepted...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.