admin管理员组文章数量:1429445
var y=document.forms["form"]["user"].value
if (y==null || y=="" )
{
alert("Username cannot be Blank");
return false;
};
var x = new Array();
x[0] = "bill";
x[1] = "ted";
x[2] = "jim";
for ( keyVar in x )
{
if (x==y)
{
alert("Username Taken");
return false;
};
};
How do I pare a variable to that in a JavaScript array, I managed to make the example above, but the second part, the bit I need, doesn't work. any ideas ?
var y=document.forms["form"]["user"].value
if (y==null || y=="" )
{
alert("Username cannot be Blank");
return false;
};
var x = new Array();
x[0] = "bill";
x[1] = "ted";
x[2] = "jim";
for ( keyVar in x )
{
if (x==y)
{
alert("Username Taken");
return false;
};
};
How do I pare a variable to that in a JavaScript array, I managed to make the example above, but the second part, the bit I need, doesn't work. any ideas ?
Share Improve this question asked Dec 2, 2011 at 13:32 Rudiger KiddRudiger Kidd 4961 gold badge6 silver badges23 bronze badges3 Answers
Reset to default 4You can simply check an array with the Array.prototype.indexOf
method.
var x = [ 'bill', 'ted', 'jim' ],
y = 'ted';
if( x.indexOf( y ) > -1 ) {
alert('Username Taken');
return false;
}
You should use an object instead:
var y = document.forms["form"]["user"].value;
if (y == null || y == "") {
alert("Username cannot be Blank");
return false;
};
var x = {};
x["bill"] = true;
x["ted"] = true;
x["jim"] = true;
if (x[y] === true) {
alert("Username Taken");
return false;
}
You can do this quite simply with jQuery
The list of the array values
var x = new Array();
x[0] = "bill";
x[1] = "ted";
x[2] = "jim";
then
if(jQuery.inArray("John", arr) == -1){
alert("Username Taken");
}
本文标签: How to check if a username is takenby checking against a JavaScript arrayStack Overflow
版权声明:本文标题:How to check if a username is taken, by checking against a JavaScript array. - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745543470a2662599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论