2011-03-24 101 views
1

我有一个表单的javascipt函数。该代码是:Javascript验证多个功能?

<script type="text/javascript"> 
function verify() { 
    if (isNaN(document.form1.exp_amount.value) == true) { 
     alert("Invalid Block Amount"); 
     return false; 
    } else if ((document.form1.exp_name.value).length == 0) { 
     alert("Block Exp is left Blank!"); 
     return false; 
    } else if ((document.form1.exp_amount.value).length == 0) { 
     alert("Block Amount is left Blank!"); 
     return false; 
    } else { 
     document.form1.submit(); 
     return true; 
    } 
} 
</script> 

,现在我必须为它提供字母验证,我有它在单独的JS功能:

<script language="javascript" > 
function checkName() { 
    re = /^[A-Za-z]+$/; 
    if (re.test(document.exp_name.form1.value)) { 
     alert('Valid Name.'); 
    } else { 
     alert('Invalid Name.'); 
    } 
} 
</script> 

如果我想有字母验证内部功能验证() ..我怎么能这样做?或者还有其他方法吗?

+0

你有没有想过你的缩进代码? – ThiefMaster 2011-03-24 07:26:26

回答

0
<script type="text/javascript"> 
function verify() 
{ 
    if(isNaN(document.form1.exp_amount.value)==true) 
    { 
     alert("Invalid Block Amount"); 
     return false; 
    } 
    else if((document.form1.exp_name.value).length==0) 
    { 
     alert("Block Exp is left Blank!"); 
     return false; 
    } 
    else if((document.form1.exp_amount.value).length==0) 
    { 
     alert("Block Amount is left Blank!"); 
     return false; 
    } 
    else if(!(/^[A-Za-z]+$/.test(document.form1.exp_amount.value))) //ADD THIS 
    { 
     alert('Invalid Name'); 
     return false; 
    } 

    document.form1.submit(); 
    return true; 
} 
</script> 
+0

由于,表单名称,字段名称未提供用于字母验证。它会起作用吗? – Coolbreeze 2011-03-24 06:45:04

+0

@Dinzy对不起,这是复制粘贴错误:D – TheVillageIdiot 2011-03-24 06:47:41

+0

@village,我试过你的方式,它没有显示警报框!它提交的价值!.. :(表单行动='blockexp.php'名称='form1'方法='POST'onsubmit ='返回验证()'>< - 我有这在表单 – Coolbreeze 2011-03-24 06:53:17

0

只需将checkName函数中返回或真或假:

function checkName() 
{ 
    re = /^[A-Za-z]+$/; 
    if(re.test(document.exp_name.form1.value)) 
    { 
     alert('Valid Name.'); 
     return true; 
    } 
    else 
    { 
     alert('Invalid Name.'); 
     false; 
    } 
} 

然后调用它,并检查结果。

... 

    else if((document.form1.exp_amount.value).length==0) 
    { 
     alert("Block Amount is left Blank!"); 
     return false; 
    } 

    else if (!checkName()) { 
     return false; 
    } 

    else 
    { 
     document.form1.submit(); 
     return true; 
    } 

另外,有很多方法可以清理和改进代码。我现在不想进入他们,但如果您想讨论它,请留下评论。

+0

当然..我想如果所有的验证都通过,应该提交 – Coolbreeze 2011-03-24 06:44:07

0

编辑您的检查名()函数来

function checkName() 
{ 
re = /^[A-Za-z]+$/; 
if(re.test(document.exp_name.form1.value)) 
{ 
    alert('Valid Name.'); 
    return true; 
} 
else 
{ 
    alert('Invalid Name.'); 
    return false; 

} 

} 

并添加

else if(!checkName()){ return false;} 

到您的验证码只是形式之前提交

+0

表单,但即使检查通过,return checkName()也不会提交,因为它会返回true – TheVillageIdiot 2011-03-24 06:49:45

+0

是的......我会相应地更新代码!! – 2011-03-24 07:12:56

1

请改变你的验证,形成以这里面会允许提交表格如果有效,而不是如果错误。下面的代码在我看来是规范,并且可以在支持正则表达式的所有浏览器上工作(它是在1996年用NS3.0在JS1.1中引入的) - 请注意,JavaScript不支持名称中的破折号,除非您引用字段名称在脚本中。代码并不需要形式来命名,因为它通过在呼叫(本)形式对象,并使用对象的功能theForm

<html> 
<head> 
<title>Canonical forms validation without jQuery</title> 
<script type="text/javascript"> 
var validName = /^[A-Za-z]+$/; 
function checkName(str) { 
    return validName.test(str); 
} 

function verify(theForm) { 
    // note: theForm["..."] is short for theForm.elements["..."]; 
    var amount = theForm["exp_amount"].value; 
    if(amount ==""){ 
    alert("Block Amount is left blank"); 
    theForm["exp_amount"].focus(); 
    return false; 
    } 
    if (isNaN(amount)) { 
    alert("Invalid Block Amount"); 
    theForm["exp_amount"].focus(); 
    return false; 
    } 

    var name = theForm["exp_name"].value; 
    if(name.length==0) { 
    alert("Block Exp is left Blank!"); 
    theForm["exp_name"].focus(); 
    return false; 
    } 
    if(!checkName(name)) { 
    alert("Block Exp is invalid!"); 
    theForm["exp_name"].focus(); 
    return false; 
    } 
    return true; 
} 
</script> 
</head> 
<body> 
<form onsubmit="return verify(this)"> 
Amount: <input type="text" name="exp_amount" value="" /><br /> 
Name: <input type="text" name="exp_name" value="" /><br /> 
<input type="submit" /> 
</form> 
</body> 
</html> 
+0

感谢您的回复,我的输入文本字段为支出名称(exp_name-文本),块金额(exp_amount-Integer),表单名称为form1。您可以在编码中编辑此信息。我是新来的,所以这个我可以更清楚地了解脚本中发生了什么。欢呼声 – Coolbreeze 2011-03-24 07:10:31

+0

@Dinzy - 当然。如果你的名字中有破折号,那么代码会改变。请编辑您的问题的文本,以反映这种情况下的实际情况。我会更新我的代码。我的代码不需要表单的名称,因为我们在验证函数中传递表单对象 – mplungjan 2011-03-24 07:16:13

+0

@Dinzy -integer或-Integer?套管很重要! – mplungjan 2011-03-24 07:24:25