【问题标题】:Javascript to ckeck if user enters space in the text fieldJavascript 检查用户是否在文本字段中输入空格
【发布时间】:2012-10-23 18:39:26
【问题描述】:

我想要一个脚本,它应该在文本字段中输入空格时提醒用户。我认为它需要 javascript。我正在编写一个脚本,我不希望用户在其中输入空格在文本字段中。

【问题讨论】:

  • 到目前为止你做了什么?你被什么困住了?
  • 在我创建的注册表单中,我不希望在用户名中输入空格

标签: javascript textbox space prompt


【解决方案1】:

以下功能起作用

$returnValue = preg_match('/[^a-z^A-Z^0-9]/', $str, $matches);
if ($returnValue==1) 
{
    alert("spaces & symbols are not allowed");
}

注意:不仅限制空格,还限制符号。

【讨论】:

    【解决方案2】:

    使用字符串的match()方法

    <script type="text/javascript">
    
    function checkForm()
    
        {
        var textb=document.getElementById("textbox1").value;
    
            if(textb.match(' ')){
               alert('Spaces not allowed!');
               return false;
            }
    }
    

    【讨论】:

    • 我将如何指定要检查的文本框。
    • 致命错误:显示对未定义函数 match() 的调用。你能指定我应该在哪里写这个脚本吗?
    【解决方案3】:
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(document).ready( function() {
    $("textarea").keypress( function(e) {
    if (e.which==32){
    alert("spaces are not allowed");
    }
    });
    });
    </script>
    <textarea></textarea>
    

    【讨论】:

    • man 它提醒说不允许有空格,但 textarea 中包含空格:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2014-01-03
    • 2021-08-18
    • 1970-01-01
    • 2012-04-23
    相关资源
    最近更新 更多