English 中文(简体)
如果字段仅是文本,需要简单的帮助验证
原标题:Need help simply validating if a field is text only
  • 时间:2012-05-24 17:53:21
  •  标签:
  • javascript

现在我有(这是来自另一个已经完成的职能)

function validatelength() {
    var length = parseInt(document.getElementById("length").value, 10);
    var lengthError = document.getElementById("lengthError");
    if (isNaN(length) || length < 50 || length > 220) {
        lengthError.innerHTML = ">>Please enter your height<<";
        return false;
    } else {
        lengthError.innerHTML = "";
    }
    return true;
}

这是我的小代码 检查字段是否包含一个人的长度。

Now for a text field (name/pre-name/notes) I want this too So far I have

function validatetext() {
    var text = parseInt(document.getElementById("text").value, 10);
    var textError = document.getElementById("textError");
    if (isNaN(text) || text < 50 || text > 220) {
        textError.innerHTML = ">>Please enter text only<<";
        return false;
    } else {
        textError.innerHTML = "";
    }
    return true;
}

Could anyone help me completing the function? Thanks BTW: I can t use jquery. (not allowed to)

问题回答

使用 regex 以确保在值中只出现字母。

类似的东西

/}[A-Za-z]*$/

应该可以工作。

从线条开始到线条结束 从0到无限的时间 从A -Z和A

我假设你不想接受 任何不是信件的东西

假设 textError 是您重新评价的字符串, 并且应该只使用文本, 我建议 :

if (textError.match(/d/)){
    // there s numbers in this string
}

JS 概念的中继证明

参考文献:

你看,这个执行可以帮助你整个生命, 如果你是学生, 解释比通常表达方式容易得多。

//In this case I will define here the lower and upper case alphabet
//you can restrict this alphabet to match commas, spaces, or anything else, you just
//need to add them to the var.

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü"; 
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ";

//This function will check if an specific letter "c" exist in the previous defined
//alphabet
function isLetter (c) 
{ 
    return( ( uppercaseLetters.indexOf( c ) != -1 ) || 
            ( lowercaseLetters.indexOf( c ) != -1 ) ) 
} 


//this is the Main Method, basically their function is split the Word "s" in all chars
//and check if those chars are defined in 
//the initials vars lowercaseLetters and uppercaseLetters

function isAlphabetic (s) {
   var i; 


    for (i = 0; i < s.length; i+=1) 
    {    
        // Check that current character is letter. 
        var c = s.charAt(i); 

        if (!isLetter(c)) 
        return false; 
    } 
    return true; 
} 

现在,要将此函数称为非常容易的函数, 您只需要从输入中捕捉到文本, 然后调用“ 输入( 文本) ” ( 文本), 文本就是所捕捉的文本 。

<SCRIPT LANGUAGE="JavaScript">

function checkIt(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (!((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))) {
       document.getElementById("lengthError").innerHTML = "This field accepts numbers only."
        return false
    }
    document.getElementById("lengthError").innerHTML = ""
    return true
}

</SCRIPT>

<INPUT TYPE="text" NAME="text" onKeyPress="return checkIt(event)">




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签