2013-10-29 60 views
1

我在我的asp.net mvc web應用程序內有以下視圖,根據輸入的IP和/或MAC進行搜索: -如何實現一個必需的驗證,以檢查用戶是否輸入至少一個值

@using (Ajax.BeginForm("AdvanceSearchIndex","Home", 
new AjaxOptions 
{ 
    HttpMethod = "get", 
    InsertionMode = InsertionMode.Replace, 
    LoadingElementId = "progress", 
    UpdateTargetId = "SearchTable" 
})) 
{ 


    <div> 
    <span class="f">IP Address </span> 

    @Html.EditorFor(model=>model.ip) 
    @Html.ValidationMessageFor(model => model.ip) 
</div> 

     <div> 
    <span class="f">MAC Address </span> 

    @Html.EditorFor(model=>model.mac) 
    @Html.ValidationMessageFor(model => model.mac) 
</div>@* 



    <input class="btn btn-success" type="submit" value="search" /> 

    <img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" /> 

    } 

但有沒有辦法來檢查用戶是否輸入IP或MAC或兩者都點擊搜索按鈕時,我需要一種方法來檢查這一個客戶端和服務器端。誰可以建議?

+0

研究javscript/jQuery驗證的客戶端。可 – charlietfl

回答

0

驗證IP:http://jsfiddle.net/kDXmd/

function ValidateIPaddress(ipaddress) 
{ 
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) 
    { 
    return (true) 
    } 

    return (false) 
} 

MAC地址http://jsfiddle.net/vY95y/

function macAddressValidation(macAdd) 
{ 
    var RegExPattern = /^[0-9a-fA-F:]+$/; 

    if (!(macAdd.match(RegExPattern)) || macAdd.length != 17) 
    { 
    return false; 
    } 
    else 
    { 
    return true; 
    } 
} 
+0

許多插件我寫了下面的腳本。但即使兩個字段爲空,警報也不會啓動?你能建議嗎? –

+0

@JohnJohn - 你可以打勾「V」我看到它是爲你工作。謝謝 –

相關問題