I want my email form filed to be validated by jQuery.
if user typed val() matches any values in array, i want to run some function before submitting.
ex. if I type
abc.com is not in array. so OK.
bbb.net is in array. so NG.
[email protected] part of string is in array. so NG.
[email protected]. i do not care... let PHP decide if its valid email. so javascriptly OK.
html:
<form action="./" method="post" id="test">
email address: <input type="text" id="mail">
<input type="submit" val="SUBMIT " id="subm">
</form>
javascript:
var arr = [
'aaa.ne.jp', 'bbb.net', 'ccc.co.uk', 'ddd.co.kr'
];
$('#test').on('submit', function() {
var _value = $('#mail').val();
if( $.inArray(_value, arr) > 0 || _value == '') {
console.log(_value + ' cant be accepted');
return false;
} else {
console.log(_value + ' is GO!');
return false;//do not submit just for the sake of example.
}
});
i used jquery's inArray() method, but somehow ,for example, [email protected] gets through the validation even though ccc.co.uk is in array.
any solution will be appreciated. thanx.