Javascript帮助-防止文本框中出现重复字符 [英] Javascript Help - prevent duplicate characters in textbox

查看:124
本文介绍了Javascript帮助-防止文本框中出现重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助来修改一些已经正常运行的JavaScript.

Looking for a bit of help to modify some already-working JavaScript.

我正在使用一段JavaScript以及一个过滤的文本框,该过滤的文本框仅允许{1234567890,-}

I am using a piece of JavaScript along with a filtered textbox, the filtered text box only allows {1234567890,-}

下面的JavaScript可以正常工作,并防止用户输入多于1个小数 我还想在允许的字符中添加破折号/减号{-}并防止重复

The below JavaScript works and prevents the user from entering more than 1 decimal I want to also add a dash/minus {-} to the allowed characters and prevent duplicates

    <script type="text/javascript">

    function PreventDuplicateDecimal(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        var itemdecimal = evt.srcElement.value.split('.');
        if (itemdecimal.length > 1 && charCode == 46)
            return false;
        return true;
    }
</script>

我尝试添加;

我尝试添加 var itemdash = evt.srcElement.value.split('-'); 并将其添加为或"语句,但没有运气

I have tried adding var itemdash = evt.srcElement.value.split('-'); and adding it as a 'or' statement but no luck

任何帮助将不胜感激

先谢谢了 保罗

推荐答案

if(evt.srcElement.value.indexOf(".") > -1) {
    if (charCode === 46) {
        return false;
    }
}
if(evt.srcElement.value.indexOf("-") > -1) {
    if (charCode === 45) {
        return false;
    }
}
return true;

这篇关于Javascript帮助-防止文本框中出现重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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