0

I have seen some other threads like this but they don't seem to help me in my problem. I am new to Javascript and I am struggling to understand which value in the email input has to be called in my javascript function to make it work.

<input type="email" name="myEmail"  value="" class="form-control" required placeholder="Please Enter your email" maxlength="50">

The function I am using is this:

function check_v_mail('myEmail') {
    fld_value = document.getElementById(field).value;
    is_m_valid = 0;
    if (fld_value.indexOf('@') >= 1) {
        m_valid_dom = fld_value.substr(fld_value.indexOf('@')+1);
        if (m_valid_dom.indexOf('@') == -1) {
            if (m_valid_dom.indexOf('.') >= 1) {
                m_valid_dom_e = m_valid_dom.substr(m_valid_dom.indexOf('.')+1);
                if (m_valid_dom_e.length >= 1) {
                    is_m_valid = 1;
                }
            }
        }
    }
    if (is_m_valid) {
        update_css_class(field, 2);
        m_valid_r = 1;
    } else {
        update_css_class(field, 1);
        m_valid_r = 0;
    }
    return m_valid_r;
}

This function is saved as email_script.js and is called in my footer as follows:

<script src="includes/js/email_script.js"></script>

What am I doing wrong?

4 Answers 4

1

You need to call the check_v_mail function. Usually, this is done when the user clicks the button to submit the form.

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

Comments

0
<form name="myForm" onsubmit="return check_v_mail('myEmail')" method="post">
Email: <input type="email" name="myEmail"  value="" class="form-control" required placeholder="Please Enter your email" maxlength="50">
<input type="submit" value="Submit">
</form>

You can add it in form's onsubmit

4 Comments

Ah that makes sense but don't i have to add a parameter in 'check_v_mail(parameter)' ?
Also you can use getElementByName since you have the name as 'myEmail'
onsubmit="return check_v_mail('MyEmail')" <input type="email" name="myEmail" id="MyEmail" value="" class="form-control" I tried this but still no success any idea?
check this jsbin : jsbin.com/punadereva/1/edit it works fine.Which means error is in your javascript function which I havent checked yet
0

In addition to calling the function on form submit or field exit, you also need to add id attribute to your element because your function uses getElementById() to get the element but your input element doesn't have id attribute

<input type="email" name="myEmail"  id ="myEmail" value="" class="form-control" required placeholder="Please Enter your email" maxlength="50">

2 Comments

I added a id="MyEmail" in the email input line, --- I also called it like that onsubmit="return check_v_mail('MyEmail') - but still it doesnt seem to work
@coolrider: change your function from function check_v_mail('myEmail') to function check_v_mail(field)
0

To expand on a comment by Mani:

Right now the signature of your function check_v_mail is incorrect, it is written like a call to a function.

Signature in email_script.js could be:

function check_v_mail(emailFieldId) {
   fld_value = document.getElementById(emailFieldId).value;

   return m_valid_r;
}

Calling on the form could be:

<form onsubmit="return check_v_mail('myEmail');" method="post"></form>

A slight tweak is to have a validate function or class that handles the validation. This can be expanded to call multiple different validation methods and decreases the likelihood that the onsubmit will become complex with additional validations.

<form onsubmit="return validateForm();" method="post"></form>
function validateForm() {
    var emailValid = check_v_mail('myEmail')
    var firstNameValid = validateFirstName('firstNameInput');

    return emailValid && firstNameValid;
}

Comments

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.