[英]Validate username onchange
我想在我的jsp页面上验证我的用户名。 基本上我想看看是否有匹配的域我想重定向到其他URL。 以下是我的功能。
function validateName()
{
var UserName=document.getElementById('Username').value;
if (UserName == "@test.com")
{
window.location="http://test.test.com";
}
else if (UserName == "@test1.com")
{
window.location="https://test.test1.com";
}
return true
}
我的表单有onchange事件。
<p>
Enter the UserName: <input id="Username" name="Username" onchange="validateName()"><br>
</p>
我怎样才能验证这一点? 我看不到上面的工作。 每当我尝试放一些“test.com”或“test1.com”时,我都没有看到表格中发生的任何事情。
如果你能对此有所了解,我将不胜感激。
使用onkeyup="validateName()"
而不是onchange="validateName()"
var name
,以前用作object id
的相同字符串... 您要使用的object ID
和var name
必须不同(不区分大小写) 。
// This:
var UserName = document.getElementById('Username').value;
\ \
\_ Var Name \
\_ Object Property
// is the same as:
var UserName = Username.value;
\ \
\_ Var Name \
\_ Object Property
有了这个, (记住,不区分大小写)你的var name
与你的object ID
有冲突,所以在你的函数中,你实际上是在比较:
if ( username == username.value ) {...}
\ \
\_ Object \
\_ Object Property
// will never be true
所以,简而言之......
用其他东西改变你的函数中的var名称。
function validateName() { var UserNameToCheck = document.getElementById('Username').value; if (UserNameToCheck == "@test.com") { alert("http://test.test.com"); //window.location="http://test.test.com"; } else if (UserNameToCheck == "@test1.com") { alert("http://test.test1.com"); //window.location="https://test.test1.com"; } return true }
<p> Enter the UserName: <input id="Username" name="Username" onchange="validateName()"><br> </p>
......或者根本不使用它并直接检查......
function validateName()
{
if ( Username.value == "@test.com" ) {...}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.