我有一个受验证码限制的表单。 恩。 userName必须包含字母字符。 因此返回真或假。
function isAlphapet()
{
var alphaExp = /^[a-zA-Z]+$/;
var namee=document.validation.userName.value;
var nalt=document.getElementById('name1');
if(namee!="")
{
if(!namee.match(alphaExp))
{
nalt.innerHTML="<font color='red'> Invalid Name: " + document.validation.userName.value + "</font>";
document.validation.userName.focus();
document.validation.userName.value="";
//This will remove the success class if the user tries to modify it incorrectly.
document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
//This is calling the css class name validationError to color the text box border in red if there is an error.
//Leaving the replacing attribute empty incase the user hasn't input the correct string.
document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
return false;
}else{//if it's validated correctly
nalt.innerHTML="";
//The text box should be green.
document.getElementById("userName").className = document.getElementById("userName").className + " validationSuccess";
return true;
}
}
else if(namee.length==0) { //if the user leaves it blank.
nalt.innerHTML="<font color='red'> Enter Name</font>";
document.getElementById('name1').focus();
document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
//The above is explained on line 27+.
return false;
}
}
并且在userName文本框的表单中我有:
Your Name:<br /><input name="userName" id="userName" onBlur="isAlphapet()" type="text" size="20" maxlength="25" /><br />
一旦上面的返回true,我需要启用我的按钮。但出于某种原因,当我尝试在体内的JScript中使用它时,isAlpet并没有返回任何内容。
有没有人有解决方案?
非常感谢。
修改:
感谢您回复Srini。
嗯。在进行更改后,整个验证功能都没有停止工作。我想要实现的是比它听起来更简单。我需要一个if语句或类似的东西:
if (isAlphapet()== true && emailvalid()== true && browserValid()==true){
document.validation.sumb.disabled=false;
}
else{
document.validation.subm.diabled=true;
}
验证是我的表单的名称。我试图在标题和正文中使用它。它无法识别我的函数的返回值。但是按钮禁用和启用仅适用于/ form标记之后
答案 0 :(得分:0)
尝试这个
<html>
<head>
<script type="text/javascript">
function isAlphapet()
{
var alphaExp = /^[a-zA-Z]+$/;
var namee=document.getElementById('userName').value;
var nalt=document.getElementById('name1');
if(namee!="")
{
if(!namee.match(alphaExp))
{
alert(namee);
nalt.innerHTML="<span style='color:red;'> Invalid Name: " + namee + "</span>";
document.validation.userName.focus();
document.validation.userName.value="";
//This will remove the success class if the user tries to modify it incorrectly.
document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
//This is calling the css class name validationError to color the text box border in red if there is an error.
//Leaving the replacing attribute empty incase the user hasn't input the correct string.
document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
return false;
}
else{//if it's validated correctly
nalt.innerHTML="";
//The text box should be green.
document.getElementById("userName").className = document.getElementById("userName").className + " validationSuccess";
return true;
}
}
else if(namee.length==0) { //if the user leaves it blank.
nalt.innerHTML="<font color='red'> Enter Name</font>";
document.getElementById('name1').focus();
document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
//The above is explained on line 27+.
return false;
}
}
</script>
</head>
<body>
<div style="float:left">
<form>
Your Name:<br /><input name="userName" id="userName" onBlur="isAlphapet()" type="text" size="20" maxlength= "25" /><br />
</form>
</div>
<div id="name1" style="float:left"></div>
</body>
</html>