仅通过使用javascritp代码允许在第一位使用+或- [英] Allow + or - in first place only by using javascritp code

查看:72
本文介绍了仅通过使用javascritp代码允许在第一位使用+或-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我写的一次允许+的代码.但是问题是,我想每次都将+放在第一位

例如:我输入456.

我只想这样+456.

但是无论我写的是什么代码,代码都允许4 + 56、45 + 6 ...在任何可以输入+号的地方都可以接受.但是我的要求是只允许放在首位.同样也要放在
.

例如,我使用+符号表示-不应该被允许使用,或者我使用-表示+表示不应该被使用.

Hi,

This is the code i wrote for allowing + in one time.But what''s the problem is , i want to assign + in first place on every time

ex: i enter the num 456.

i want like this +456 only allowed.

But whatever i wrote code that code allows 4+56, 45+6... in any where to enter the + symbol it''s accepted. But my requirement is it''s allowed only in first place.simillarly in - also.

for ex i''m using + symbol means - should not be allowed or i''m using - means + should not be allowed.

if (event.keyCode == 43) {
            if (element.value.indexOf('+', 0) == -1) {
                event.returnValue = true;
                return;
            }
            }
            else if (event.keyCode == 45) {
            if (element.value.indexOf('-', 0) == -1) {
                event.returnValue = true;
                return;
            }
            }
            event.returnValue = false;

推荐答案

尝试一下.
if(obj.value.length == 0) {
  // 107 = numeric pad '+' key 
  // 109 = numeric pad '-' key
  if(event.keyCode == 107 || event.keyCode == 109) {
    return true;
  }
  else 
    return false;
}
else if(event.keyCode == 107 || event.keyCode == 109) {
  return false;
}



问候,
尼拉·索尼(Nial Soni)



Regards,
Niral Soni


这篇关于仅通过使用javascritp代码允许在第一位使用+或-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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