2011-03-24 47 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