我有一个带有自动完成选项的textarea,用于邮件ID,我想检查邮件ID的最后提取术语的有效性。我想在输入符号逗号','之后进行验证。此textarea允许键入并自动完成邮件ID。 我想知道在自动完成代码中的何处添加验证代码
$( function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#mail_receipient" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
}).autocomplete({
minLength: 3,
appendTo: "#myModal",
source:function( request, response ) {
$.getJSON( "search/autocomplete", {
term: extractLast( request.term )
}, response );},
focus: function() {
// prevent value inserted on focus
return false;
},
autoFocus:true,
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
} });
} );
var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
if (testEmail.test(term)) alert('passed'); //in last extract after typing', '
else alert('failed');