我必须检查表单字段在用户输入开始时是否包含“@”以及它是否包含它。它可以很好地检查它是否位于字符串的开头。但是当我添加检查输入是否包含“@”时。它失败。这是我的代码
function email_valid(field)
{
var apos=field.update.value;
apos=apos.indexOf('@');
if (apos>0 ||((apos.contains('@')== 'FALSE')))
{ alert('plz enter valid input');
return false;
}
else
{ return true; }
}
编辑
这种形式的函数会检查@是否位于第一个位置以及第二个位置是否在输入中。
function @_valid(field)
{
var ref=field.update.value;// I needed ref 4 other things
var apos=ref.indexOf('@');
if (apos>=0 )
{
if (apos==0)
{
return true;
}
else { field.t_update3.value="";
alert('plz enter a valid refernce');
return false;
}
}
else { field.t_update3.value="";
alert('plz enter a valid refernce');
return false;
} }
最佳答案
考虑:
var apos = value.indexOf('@');
if (apos >= 0) {
// was found in string, somewhere
if (apos == 0) {
// was at start
} else {
// was elsewhere
}
} else {
// not in string
}
和
var apos = value.indexOf('@');
if (apos == 0) {
// was at start
} else if (apos > 0) {
// was elsewhere
} else {
// not in string
}
关于javascript 验证在用户输入开始时检查 @ : not email validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4371628/