简体   繁体   English

Javascript正则表达式仅在字符串开头检查数字和减号

[英]Javascript regex to check only numbers and minus at the start of string

Hello I am trying to make a regex in this input field to accept only 4 numbers not greater than 2934, but accept a negative one too (-2394). 您好,我正在尝试在此输入字段中创建一个正则表达式,以仅接受不大于2934的4个数字,但也接受负数(-2394)。

I want to allow the minus sign only at the start of the string, and then 4 numbers only, not greater than 2934 (positive and negative, because they are coordinates. 我只想在字符串的开头允许减号,然后只允许4个数字,不大于2934(正负),因为它们是坐标。

Already tried the given solutions here, but I am missing something. 已经在这里尝试了给定的解决方案,但我错过了一些东西。

 function numonly(myfield, e, dec){ // max input value of 4196 x 4196 $("#mlat,#mlon").keyup(function() { var val = $(this).val().replace(/[^0-9]+/,""); if (val => 2934){ !/^\\s*$/.test(val); val = (parseInt(val) > 2934) ? 2934 : val; } else { (!/^\\s*$/.test(val)); val = (parseInt(val) > 2934) ? 2934 : val; } $(this).val(val); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="mlat" type="text" name="mlat" maxlength="4" onkeypress="return numonly(this,event)"><br> <input type="text" name="mlon" id="mlon" maxlength="4" onkeypress="return numonly(this,event)"> 

I am trying to make a regex in this input field to accept only 4 numbers not greater than 2934, but accept a negative one too (-2394). 我正在尝试在此输入字段中创建一个正则表达式,以仅接受不大于2934的4个数字,但也接受一个负数(-2394)。

No need for regex here, use 这里不需要正则表达式,使用

var minValue = -2394;
var maxValue = 2934;    
var val = +$(this).val().replace(/[^0-9-]+/g,""); //using your own regex to replace non-numeric characters
var isValid = !isNaN( val ) && val < maxValue && val > minValue ;

Also, i noticed that you want to make these values as upper and lower bound 另外,我注意到您想将这些值作为上限和下限

if ( !isNaN( val ) )
{
    var finalVal  = isValid ? val : (val < 0 ? minValue  : maxValue);
}
function numonly(myfield, e, dec){
  // max input value of 4196 x 4196
  $("#mlat,#mlon").keyup(function() {
    var val = $(this).val().replace(/[^0-9-]+/,"");
    if (parseInt(val) > 2934){
      val = (parseInt(val) > 2934) ? 2934 : val;
    }
    else {
      val = (parseInt(val) < -2934) ? -2934 : val;
    }
    $(this).val(val);
  });
}

Increase your max length to 5 to cater the '-' 将您的最大长度增加到5以迎合“-”

<input id="mlat" type="text" name="mlat" maxlength="5" onkeypress="return numonly(this,event)"><br>
<input type="text" name="mlon" id="mlon" maxlength="5"  onkeypress="return numonly(this,event)">

you can use the following method : 您可以使用以下方法:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
<input id="mlat" type="text" name="mlat" maxlength="5" onkeypress="return numonly(this,event)"><br>
<input type="text" name="mlon" id="mlon" maxlength="5"  onkeypress="return numonly(this,event)">
<script type="text/javascript">

function numonly(myfield, e, dec){

  // max input value of 4196 x 4196
  $("#mlat").keyup(function() {
    var val = $(this).val().replace(/-?\d+[^0-9]+/,"");
    if (val => 2934){
      !/^\s*$/.test(val);
      if (val > 0) {
        val = (parseInt(val) > 2934) ? 2934 : val;
      }else{
        val = (parseInt(val) > -2394) ? val : -2394;
      }

    }
    else {
      (!/^\s*$/.test(val));
      if (val > 0) {
        val = (parseInt(val) > 2934) ? 2934 : val;
      }else{
        val = (parseInt(val) > -2394) ? val : -2394;
      }
    }
    $(this).val(val);
  })
};


</script>
</body>
</html>

and replace the maxlength to 5 to handle - sign in input tag. 并将maxlength替换为5以进行处理-登录输入标签。

暂无
暂无

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

相关问题 jQuery RegEx仅以加号或减号和数字开头 - Jquery RegEx start by plus or minus and numbers only 正则表达式仅检查开始和结束以及数字 - Regex to check start & end and numbers only 如何在javascript中检查字符串是否只包含数字,逗号和带正则表达式的句点? - How can I check if a string contains only numbers, comma and periods with regex in javascript? JavaScript Regex - 如何检查字符串是否仅包含字母、数字、破折号、下划线和点 - JavaScript Regex - How to check if a string only contains letters, numbers, dashes, underscores and dots Javascript正则表达式可从字符串开头删除数字以外的任何内容 - Javascript regex to remove anything but numbers from the start of a string 负数只有一个减号 JS 正则表达式 - Only one minus symbol for negative numbers JS regex 正则表达式JavaScript-如何检查字符串是否全是字母,然后是所有数字 - Regex JavaScript - how to check if a string is all letters and then all numbers Javascript:如何检查字符串中是否存在两个数字? (正则表达式) - Javascript: How to check if two numbers exist in string? (regex) 如何使用javascript中的regex检查字符串的开头是否具有特定字符? - How to check if the start of a string has specific character using regex in javascript? 用于测试字符串是否在分隔行上只有数字的 Javascript 正则表达式 - Javascript Regex to test if string has only numbers on separated lines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM