admin管理员组文章数量:1430093
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
2 Answers
Reset to default 3There 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
版权声明:本文标题:javascript - How do I check if an input field value contains a character? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745552240a2662995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论