2015-04-02 108 views
0

我已經寫了相同的JavaScript兩次,兩個文本框,但它同時運行一個文本框腳本我需要編寫一次,調用相同的多個文本框JavaScript驗證的onblur

function checkalphabets() { 
    var pattern = /^[a-zA-Z\s]+$/; 
    if (!pattern.test(myTextBox.value)) { 
     modal({ 
      type: 'warning', 
      title: 'Warning', 
      text: 'Only Alphabets allowed!', 
      center: false, 
      callback: function(){ $("#myTextBox").focus();} 
     }); 

     exit; 
     return false; 
    } 
    return true; 

} 
function checkalphabets1() { 
    var pattern = /^[a-zA-Z\s]+$/; 
    if (!pattern.test(TextBox1.value)) { 
     modal({ 
      type: 'warning', 
      title: 'Warning', 
      text: 'Only Alphabets allowed!', 
      center: false, 
      callback: function(){ $("#TextBox1").focus();} 
     }); 

     exit; 
     return false; 
    } 
    return true; 

} 
$('.modal-btn').click(function() { 

     $('#modal-window').hide(); 

    }); 

這裏是fiddle

+0

您應該在嘗試提取它們的值之前定義myTextBox和TestBox1。 – 2015-04-02 07:39:32

回答

0

您可以將元素進行檢查作爲函數的參數:

function checkalphabets($el) { 
    var pattern = /^[a-zA-Z\s]+$/; 
    if (!pattern.test($el.val())) { 
     modal({ 
      type: 'warning', 
      title: 'Warning', 
      text: 'Only Alphabets allowed!', 
      center: false, 
      callback: function(){ $el.focus();} 
     }); 

     return false; 
    } 
    return true; 
} 

調用,檢查上的Elemen完成時傳遞的元素噸。

checkalphabets($("#TextBox1")); 
+0

它不工作,請檢查我的小提琴http://jsfiddle.net/7kpsv1p4/46/ – 2015-04-02 09:41:27

+0

你有你的小提琴語法錯誤。你需要在你的內聯'onblur'代碼中使用單引號''' – atmd 2015-04-02 09:44:26

+0

請檢查同時驗證的兩個文本框http://jsfiddle.net/7kpsv1p4/52/ – 2015-04-02 10:04:41

0

在您的代碼中textbox1和mytextbox是未定義的。通過要素的功能,使他們定義:

function checkalphabets(myinput) { 
    var pattern = /^[a-zA-Z\s]+$/; 
    if (!pattern.test(myinput.value)) { 
     modal({ 
      type: 'warning', 
      title: 'Warning', 
      text: 'Only Alphabets allowed!', 
      center: false, 
      callback: function(){ $(myinput).focus();} 
     }); 

     exit; 
     return false; 
    } 
    return true; 
} 

$('.modal-btn').click(function() { 

     $('#modal-window').hide(); 

    }); 

也是我建議的的onkeyup代替的onblur進行檢查每個字符後:

<input name="myTextBox" type="text" id="myTextBox" onkeyup="checkalphabets(this)" /> 
<input name="TextBox1" type="text" id="TextBox1" onkeyup="checkalphabets(this)" /> 

ASLO這將是很好的主意減去警告後不允許的字符。

0

你能想到將這個值作爲輸入傳遞給Javascript中的函數嗎?&從下面的解釋中提取這個變量的值?

HTML:

<form method="post" action="test.aspx" id="form1"> 
<div class="aspNetHidden"> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODIwMTY2MjU3ZGQVs14nJwqB/iIms8CsXW3SCzs22w==" /> 
</div> 

<div class="aspNetHidden"> 

    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="75BBA7D6" /> 
</div> 
    <div> 
     <input name="myTextBox" type="text" id="myTextBox" onblur="checkalphabets(this)" /> 
     <input name="TextBox1" type="text" id="TextBox1" onblur="checkalphabets(this)" /> 
    </div> 
    </form> 

的Javascript:

function checkalphabets(thisValue) { 
    var pattern = /^[a-zA-Z\s]+$/; 
    if (!pattern.test(thisValue.value)) { 
     modal({ 
      type: 'warning', 
      title: 'Warning', 
      text: 'Only Alphabets allowed t1 & t2!', 
      center: false, 
      callback: function(){ $(thisValue).focus();} 
     }); 

     exit; 
     return false; 
    } 
    return true; 
} 
$('.modal-btn').click(function() { 
     $('#modal-window').hide(); 
    }); 

喜歡聰明人,你可以使用任意數量的文本框的同樣的方法!

+0

沒有它的不工作兩個文本框都是相同的驗證時間 – 2015-04-02 09:13:46

+0

我在JSfiddle中運行相同的代碼,我能夠看到該方法在該特定文本框的模糊處僅被觸發一次。但是,如果我們從一個文本框1移動到另一個文本框2 ..將引發textbox1的onblur事件,這將顯示一個模式...在模型警報中點擊「確定」,將調用textbox2的模糊事件...是你正在談論的問題是什麼? – 2015-04-02 09:21:44

+0

哪裏有提及的asp.net從哪裏來? – atmd 2015-04-02 09:45:15