2011-12-07 46 views
0

警告消息,我想打一個JavaScript,將顯示一條警告消息從一個文本框個別詞。我的意思是,如果用戶寫HTTP:在textfield ///,點擊我的表單的提交按鈕,然後一個javascript將顯示一個錯誤信息給用戶。所以我怎樣才能做到這個textfield:的javascript值字段

<input type="text" name="attn" size="35" /> 

我該如何編輯它?

+3

如果你不介意[jQuery的](http://jquery.com/)我會看的到[驗證插件(http://bassistance.de/jquery-plugins/jquery-plugin -validation /)。 –

+1

請確保您檢查此客戶端和服務器端。繞過客戶端驗證檢查是微不足道的。 – mrtsherman

回答

1

當用戶提交表單,您可以執行以下操作:

//Grab the value of your textbox 
var textValue = document.getElementsByName("myInput").value; 

//Checks if the textbox contains 'http://' or '/' 
if(textValue.indexOf('http://') || textValue.indexOf('/')) 
{ 
    alert("Please check your input and try again..."); 
    return false; 
} 
2

給輸入一些id

然後,你可以這樣做:

var the_input = document.getElementById('inputID'); 
the_input.onkeyup = function(){ 
    if(the_input.value == 'http://' || the_input.value.indexOf('http://') > 0) { 
     alert('AHHH!'); 
     the_input.value = ''; 
    } 
} 

演示:http://jsfiddle.net/maniator/rdZAZ/

嘗試輸入http://輸入。

+1

很棒的提醒!哈哈哈 – 2011-12-07 17:50:32

+0

@Márcio:-D我只是更新它,所以'http://'可以在輸入的任何地方 – Neal