简体   繁体   English

限制文本区域中的空格或空格?

[英]restrict breaks or excessive spaces in text area?

Can someone please help me, i have a text area and i have limited the number of characters in it so that the text area fits the content in it without the content overflowing. 有人可以帮我吗,我有一个文本区域,并且我限制了其中的字符数,以便该文本区域适合其中的内容而不会出现内容溢出。

Character length works but this doesn't stop the user currently using the space bar to create breaks or excessive spacing, ie more than one space. 字符长度有效,但这并不能阻止用户当前使用空格键来创建分隔符或过多的空格,即超过一个空格。 is there a way i can make the text area stop users spacing characters more than once and forbid line breaks? 有没有一种方法可以使文本区域停止用户多次输入空格并禁止换行?

Sorry if this is a really obvious question but im looking for an answer everywhere and cant find one anywhere. 抱歉,如果这是一个非常明显的问题,但是我在各处都找不到答案,而在任何地方都找不到。

<form action="includes/change_status.php" id="form2" method="post" name="form2">
    <div class="status-border-top"></div>

    <div class="status-border">
        <textarea data-id="status" id="status" maxlength="80" name="status" style="width: 187px; margin-top:-4px; text-align:left; padding-left:5px; padding-top:3px; padding-bottom:2px; padding-right:3px; margin-left:-4px; height: 54px; font-size:12px; resize: none; border: hidden; -moz-border-radius: 6px; -webkit-border-radius: 6px; border-radius: 6px; position:relative; z-index:100;"><?php echo htmlspecialchars ($profile['status']); ?></textarea>
        <input class="status-submit" id="submit" name="submit" src="../PTB1/assets/img/icons/save-status.png" type="image" value="submit">
    </div>
</form>

This is how you can remove double spaces or line breaks that the user enters into the textarea . 这样可以删除用户输入到textarea中的双倍空格或换行符。

You'll need to use the latest version of jQuery for this to work. 您需要使用最新版本的jQuery才能正常工作。

Working example on jsFiddle . jsFiddle上的工作示例

HTML: HTML:

<form action="/" method="post">
    <textarea cols="33" rows="12" id="message"></textarea>
</form>

JavaScript: JavaScript的:

var $message = $("#message");

$message.on("keydown keypress", function() {    
    var $this = $(this),
        val = $(this).val()
                     .replace(/(\r\n|\n|\r)/gm," ") // replace line breaks with a space
                     .replace(/ +(?= )/g,''); // replace extra spaces with a single space

    $this.val(val);
});

Another option without jQuery is to compare the currently assigned key to the previous key. 没有jQuery的另一种选择是将当前分配的键与上一个键进行比较。 If they are the same, and both are in the set of illegal keys, refuse the keypress action. 如果它们相同,并且都在非法密钥集中,请拒绝按键操作。 This approach will not change the starting text in any way, no matter what spacing that has. 不管间距如何,该方法都不会以任何方式更改起始文本。

HTML: HTML:

<form action="includes/change_status.php" id="form2" method="post" name="form2">
    <div class="status-border-top"></div>

    <div class="status-border">
        <!-- notice the onkeypress attribute -->
        <textarea data-id="status" id="status" maxlength="80" name="status" style="width: 187px; margin-top:-4px; text-align:left; padding-left:5px; padding-top:3px; padding-bottom:2px; padding-right:3px; margin-left:-4px; height: 54px; font-size:12px; resize: none; border: hidden; -moz-border-radius: 6px; -webkit-border-radius: 6px; border-radius: 6px; position:relative; z-index:100;" onkeypress="return ignoreSpaces(event);"><?php echo htmlspecialchars ($profile['status']); ?></textarea>
        <input class="status-submit" id="submit" name="submit" src="../PTB1/assets/img/icons/save-status.png" type="image" value="submit">
    </div>
</form>

Javascript: 使用Javascript:

var lastkey;
var ignoreChars = ' \r\n'+String.fromCharCode(0);
function ignoreSpaces(e){
    e = e || window.event;
    var char = String.fromCharCode(e.charCode);
    if(ignoreChars.indexOf(char) >= 0 && ignoreChars.indexOf(lastkey) >= 0){
        lastkey = char;
        return false;
    }else{
        lastkey = char;
        return true;
    }
}

working JFiddle: http://jsfiddle.net/mLYgD/1/ 工作的JFiddle: http : //jsfiddle.net/mLYgD/1/

Without client-side scripting, you cannot impose such restrictions. 如果没有客户端脚本,则无法施加此类限制。

Since you want to allow at most 80 characters and to disallow line breaks, it would seem to be more adequate to use an input type=text element than a textarea . 由于您希望最多允许80个字符并禁止换行,因此使用input type=text元素似乎比textarea更合适。

Moreover, for input type=text , you can impose restrictions using the HTML5 pattern attribute. 此外,对于input type=text ,您可以使用HTML5 pattern属性施加限制。 Though not supported by old browsers, it works in modern browsers even when client-side scripting has been disabled. 尽管旧的浏览器不支持,但即使禁用了客户端脚本,它也可以在现代浏览器中使用。 You could use code like this: 您可以使用如下代码:

<input type="text" data-id="status" id="status" maxlength="80" size="80"
  name="status" pattern="\s?(\S+\s?)*">

The size attribute specifies the visible width of the field in (average-width) characters. size属性以(平均宽度)字符指定字段的可见宽度。 The default font face and font size in an input box depends on the browser, but usually the face is not monospace, unlike in textarea . 输入框中的默认字体和字体大小取决于浏览器,但与textarea不同,该字体通常不是等宽字体。 So if monospace font is desired, set it in CSS. 因此,如果需要等宽字体,请在CSS中进行设置。

The value of the pattern attribute has the same syntax as JavaScript regular expressions, except that the match is for the entire input string (so a leading ^ and a trailing $ are implied). pattern属性的值与JavaScript正则表达式具有相同的语法,但匹配项适用于整个输入字符串(因此,意味着前导^和结尾的$ )。 The notation \\s means any whitespace character (in this context, this means a space in practice), and \\S means any non-whitespace character, so the expression allows spaces but not in succession. 符号\\s表示任何空格字符(在此上下文中,这实际上表示空格), \\S表示任何非空格字符,因此该表达式允许空格但不能连续。 The leading part \\s allows a single leading space; 前导部分\\s允许单个主导空间; remove it if you don't want to allow that. 如果您不想这样做,请将其删除。

You can use JavaScript to implement the pattern attribute in browsers that do not natively support the attribute but have scripting enabled. 您可以使用JavaScript在本身不支持该属性但启用了脚本的浏览器中实现pattern属性。 Or you can just have JavaScript that uses the same expression (with ^ and $ added). 或者,您也可以只使用使用相同表达式的JavaScript(添加^$ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM