English 中文(简体)
拒绝某些 par号 是否接受?
原标题:Reject some number-strings that parseInt accepts?
  • 时间:2011-07-09 05:57:17
  •  标签:
  • javascript

问题在于接受一个或多个港口,它们之间有一席之地。

with help of friends here, I used this one for my answer but for example if I enter 88888 it will alert me such this thing:

88888NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN is not correct

我如何纠正这种情况

<script type="text/javascript">
    function portvalidating(field)
    {
    var output=  ;
        m=field.value;
        if(/^d{1,5}([ ]d{1,5})*$/.test(m))
        {
        var parts = m.split(   );
        for(i in parts)
        {
          var p= parseInt(parts[i]);
        if(!((0 <= p) && (p<= 65535) && !isNaN(p)))
        {
        output+=p;
        }
        }
    if(output==  )
        var dummy=1;
        else alert(output+ is not correct );
        }
        else alert( please enter a valid port! );
        }
最佳回答

Try separating your concerns of reading/writing to form data from input validation from alerting. Here is a hint on the function to validate a string of space separated integers in [0..65535]:

var getPorts = function(str) {
  var ns=(""+str).split(/s+/), ports=[], n, i;
  for (i=0; i<ns.length; i++) {
    n = parseInt(ns[i], 10);
    if (isFinite(n) && !isNaN(n)
          && (ns[i]==n) // Make sure the number is an integer.
          && (n >= 0) && (n <= 65535)) {
      ports.push(n);
    }
  }
  return ports;
};

getPorts( -1 0 NaN 123 foo 255 99999 ); // => [0, 123, 255]
问题回答

对于它的价值,我这里的“回答”。 它非常接近所公布守则,但如上所述,有差异。 填满jsfiddle

// Returns: {ports, invalid}
// where ports are valid ports and invalid are ... not valid items.
function getPorts (inp) {
    var invalid = [];
    var ports = [];
    // The /s+/ ensures that multiple whitespace is skipped.
    var split = inp.split(/s+/);
    for (var i = 0; i < split.length; i++) {
        var str = split[i];
        // It is generally best to *always* specify a base.
        // (Otherwise it might be treated as hex or octal, which may or
        // may not be okay. Adjust as required.)
        var val = parseInt(str, 10);
        // Need to make sure we have all digits.
        // This is because parseInt("1foo2", 10) evaluates to 1
        // and parseInt("8888NaN") evaluates to 8888
        if (!str.test(/^d+$/) || !(val >= 0 && val <= 0xffff)) {
            invalid.push(str);
        } else {
            ports.push(val);
        }
    }
    return {ports: ports, invalid: invalid};
}

// if r.invalid.length > 0 then it contained some "invalid" items.
// it may also be "invalid" if r.ports.length == 0

var r = getPorts("88888NaNNaN");
alert("ports: " + r.ports);
alert("invalid: " + r.invalid);

var r = getPorts("123 12345 88888 NaN NaN 1d234");
alert("ports: " + r.ports);
alert("invalid: " + r.invalid);

Happy coding.


请将此职能按每名maerics员额分列。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签