admin管理员组

文章数量:1023220

 var email = document.querySelector('#issueContactEmail');


 if (email.value.includes('@')) {
    console.log('has @');
    return valid;
}

I have a form with an input with the id of issueContactEmail

I would like to check if there is an @ symbol in the input value before the user can go forward. My code here does not seem to work.

How else can I check this? If you could briefly explain why this doesn't work that would be great too! thank you in advance

 var email = document.querySelector('#issueContactEmail');


 if (email.value.includes('@')) {
    console.log('has @');
    return valid;
}

I have a form with an input with the id of issueContactEmail

I would like to check if there is an @ symbol in the input value before the user can go forward. My code here does not seem to work.

How else can I check this? If you could briefly explain why this doesn't work that would be great too! thank you in advance

Share Improve this question asked Aug 9, 2018 at 2:57 DumbDevGirl42069DumbDevGirl42069 9695 gold badges25 silver badges55 bronze badges 3
  • Cannot reproduce. jsfiddle/fnsxt7dp Post a minimal reproducible example. Put the check inside some listener if it isn't already, don't run the check on page load – CertainPerformance Commented Aug 9, 2018 at 2:59
  • I assume you're using a somewhat modern browser, correct? IE, for example, does not support includes. – Tyler Roper Commented Aug 9, 2018 at 3:11
  • How is the code not working? are you getting any errors, perhaps indicating if the value is undefined, or whether includes exists (indexOf is an alternative) – Maurice Yu Commented Aug 9, 2018 at 3:44
Add a ment  | 

2 Answers 2

Reset to default 3

There are a few possibilities on why this code is not working for you. The first is that your javascript is running before the document has fully loaded (ie #issueContactEmail is not present in the DOM when your javascript tries to access it). Consider moving your javascript to the end of your <body> tag if possible, to eliminate that issue.

Another possibility is that the browser you're running this code in does not offer/support the .includes() method that you're trying to use (this will be the case in some older browsers)

Perhaps you could try the following code, that puts in place a few additional safety checks to avoid the later issue:

var email = document.querySelector('#issueContactEmail');
var valid = false;

if(email) { // Check if input#issueContactEmail field exists
  if(email.value && email.value.contains('@')) { // Check if field value
                                                 // exists and contains '@'

    valid = true; // Then the field value is considered valid
  }
}

return valid; // Return if the field value was considered valid

Here's a jsFiddle as well - hope this helps you!

Try this one

//Takes the value form the input field
var email = document.querySelector('#issueContactEmail');
//returns weather this is a valid input value(contains @ sysmble)
return email.value && email.value.contains('@');
 var email = document.querySelector('#issueContactEmail');


 if (email.value.includes('@')) {
    console.log('has @');
    return valid;
}

I have a form with an input with the id of issueContactEmail

I would like to check if there is an @ symbol in the input value before the user can go forward. My code here does not seem to work.

How else can I check this? If you could briefly explain why this doesn't work that would be great too! thank you in advance

 var email = document.querySelector('#issueContactEmail');


 if (email.value.includes('@')) {
    console.log('has @');
    return valid;
}

I have a form with an input with the id of issueContactEmail

I would like to check if there is an @ symbol in the input value before the user can go forward. My code here does not seem to work.

How else can I check this? If you could briefly explain why this doesn't work that would be great too! thank you in advance

Share Improve this question asked Aug 9, 2018 at 2:57 DumbDevGirl42069DumbDevGirl42069 9695 gold badges25 silver badges55 bronze badges 3
  • Cannot reproduce. jsfiddle/fnsxt7dp Post a minimal reproducible example. Put the check inside some listener if it isn't already, don't run the check on page load – CertainPerformance Commented Aug 9, 2018 at 2:59
  • I assume you're using a somewhat modern browser, correct? IE, for example, does not support includes. – Tyler Roper Commented Aug 9, 2018 at 3:11
  • How is the code not working? are you getting any errors, perhaps indicating if the value is undefined, or whether includes exists (indexOf is an alternative) – Maurice Yu Commented Aug 9, 2018 at 3:44
Add a ment  | 

2 Answers 2

Reset to default 3

There are a few possibilities on why this code is not working for you. The first is that your javascript is running before the document has fully loaded (ie #issueContactEmail is not present in the DOM when your javascript tries to access it). Consider moving your javascript to the end of your <body> tag if possible, to eliminate that issue.

Another possibility is that the browser you're running this code in does not offer/support the .includes() method that you're trying to use (this will be the case in some older browsers)

Perhaps you could try the following code, that puts in place a few additional safety checks to avoid the later issue:

var email = document.querySelector('#issueContactEmail');
var valid = false;

if(email) { // Check if input#issueContactEmail field exists
  if(email.value && email.value.contains('@')) { // Check if field value
                                                 // exists and contains '@'

    valid = true; // Then the field value is considered valid
  }
}

return valid; // Return if the field value was considered valid

Here's a jsFiddle as well - hope this helps you!

Try this one

//Takes the value form the input field
var email = document.querySelector('#issueContactEmail');
//returns weather this is a valid input value(contains @ sysmble)
return email.value && email.value.contains('@');

本文标签: javascriptHow do I check if an input field value contains a characterStack Overflow