如何使用JavaScript防止输入字段中出现空格和句号 [英] How to prevent spaces and full stops in input field with javascript

查看:64
本文介绍了如何使用JavaScript防止输入字段中出现空格和句号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下禁止空格的地方

I have the following to disallow spaces

function nospaces(t){

    if(t.value.match(/\s/g)){

        alert('Username Cannot Have Spaces or Full Stops');

        t.value=t.value.replace(/\s/g,'');

    }

}

HTML

<input type="text" name="username" value="" onkeyup="nospaces(this)"/>

它在空间上很好用,但是我怎么也不能禁止句号?

It works well for spaces but how can I also disallow full stops as well?

推荐答案

尝试一下

    function nospaces(t){
        if(t.value.match(/\s|\./g)){
            alert('Username Cannot Have Spaces or Full Stops');
            t.value=t.value.replace(/\s/g,'');
        }
    }

这篇关于如何使用JavaScript防止输入字段中出现空格和句号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆