2017-04-12 71 views
0

如何一次檢查多個變量輸入以確保正則表達式正常工作?每次我輸入任何內容時,表單都會提交併且不會提醒任何事情。無法從服務器驗證用戶輸入是否正確?

我已經試過正則表達式驗證測試()方法也和仍然沒有運氣。

我試圖驗證具有以下的正則表達式,使到什麼地方是不是數字或空格被認爲是一個錯誤的輸入用戶輸入。

var format=/^(\s*|\d+)$/; 

它只接受文本框中的數字和空格。

以下JavaScript是我:

var pitch = document.getElementById("pitch"); 
 
var chisel = document.getElementById("chis"); 
 
var saw = document.getElementById("saw"); 
 

 
//var arguments = [chisel, saw, pitch]; 
 

 
var format = /^(\s*|\d+)$/; 
 

 
function regexTest() { 
 
    if (!chisel.match(format) && !saw.match(format) && !pitch.match(format)) { 
 
    alert("Repressed Action"); 
 
    return false; 
 
    } else { 
 
    alert('Thank you'); 
 
    } 
 
}
<div class="lab"> 
 

 
    <form method="post" action="http://weblab.kennesaw.edu/formtest.php"> 
 
    Chisels: <input type="text" name="chisels" id="chis" size="5" /> Saw: <input type="text" name="saw" id="saw" size="5" /> Pitchfork: <input type="text" name="pitchfork" id="pitch" size="5" /> 
 

 
    <br /> Customer Name: <input type="text" name="customer name" size="25" /> 
 
    <br /> Shipping Address: <input type="text" name="shipping address" size="25" /> 
 
    <br /> State: 
 
    <input type="radio" id="master" name="card" value="master" /><label for="master">MasterCard</label> 
 
    <input type="radio" id="american" name="card" value="american" /><label for="american">American Express</label> 
 
    <input type="radio" id="visa" name="card" value="visa" /><label for="visa">Visa</label> 
 

 
    <br /> 
 

 
    <input type="reset" value="Reset" /> 
 

 
    <div class="lab"> 
 
     <button onclick="regexTest()">Submit</button> 
 
     <button onclick="return false">Cancel</button> 
 
    </div>

+0

對我來說,正則表達式應該是'/ [^ \ s \ d] /',它會匹配任何不是數字或空格的東西。如果您打算髮布HTML,請不要包含PHP,請發佈實際的可運行代碼。 – RobG

+0

@RobG我相信你的解決方案是正確的。 –

+0

它部分工作。如果我將所有三個輸入作爲數字,它就可以工作。但是,如果我讓他們中的一個成爲一封信,它仍然有效。我會繼續嘗試。 –

回答

0

有許多的問題與您的代碼,下面我重構它有點更容易閱讀等有用。

驗證監聽器應該位於表單的提交處理程序中,而不是提交按鈕,因爲可以在不單擊按鈕的情況下提交表單。另外,如果將表單的引用傳遞給偵聽器,則通過名稱訪問表單控件會更容易。

你應該得到的表單控件的值時,提交發生,而不是之前。你的代碼在用戶完成任何事情之前(甚至可能在表單存在之前)立即獲取這些值,因此將該代碼放入偵聽器函數中。

最後,正則表達式需要匹配任何不是空格或數字,所以:

/[^\s\d]/ 

似乎是適當的。但是,如果字段爲空(它們不包含非數字或非空格),這仍然允許表單提交。你需要爲此添加一個測試。

function regexTest(form) { 
 
    // Get values when the function is called, not before 
 
    var pitch = form.pitchfork.value; 
 
    var chisel = form.chisels.value; 
 
    var saw = form.saw.value; 
 

 
    // Test for anything that's not a space or digit  
 
    // var format = /^(\s*|\d+)$/; 
 
    var format = /[^\s\d]/; 
 

 
    if (format.test(chisel) || format.test(pitch) || format.test(saw)) { 
 
    // There must be at least one non-space or non-digit in a field 
 
    alert("Repressed Action"); 
 
    return false; 
 

 
    } else { 
 
    alert('Thank you'); 
 
    // return false anyway for testing 
 
    return false; 
 
    } 
 
}
<div class="lab"> 
 
    <form onsubmit="return regexTest(this)"> 
 
    Chisels: <input type="text" name="chisels" id="chis" size="5"><br> 
 
    Saw: <input type="text" name="saw" id="saw" size="5"><br> 
 
    Pitchfork: <input type="text" name="pitchfork" id="pitch" size="5"><br> 
 
    Customer Name: <input type="text" name="customer name" size="25"><br> 
 
    Shipping Address: <input type="text" name="shipping address" size="25"> 
 
    <br> State: 
 
    <select name="states"> 
 
     <option>Florida</option> 
 
     <option>Georgia</option> 
 
     <option>Alabama</option> 
 
    \t </select> 
 
    <br> 
 
    <input type="radio" id="master" name="card" value="master"><label for="master">MasterCard</label> 
 
    <input type="radio" id="american" name="card" value="american"><label for="american">American Express</label> 
 
    <input type="radio" id="visa" name="card" value="visa"><label for="visa">Visa</label> 
 
    <br> 
 
    <input type="reset" value="Reset"> 
 
    <div class="lab"> 
 
     <button>Submit</button> 
 
     <button onclick="return false">Cancel</button> 
 
    </div>

希望這可以讓你的下一個步驟。

+0

謝謝你的幫助!我必須注意到,我必須保持表單在提交時發送到php文件,所以我必須更改

back –

相關問題