JRS: In article <dm**********@sunce.iskon.hr>, dated Thu, 24 Nov 2005
22:51:06, seen in news:comp.lang.javascript, vili
<vi*******@hi.htnet.hr> posted :
can somebody clear what does line 2 and line3 mean??
function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; this line
if (re.test(ipaddr)) { and
this
var parts = ipaddr.split(".");
if (parseInt(parseFloat(parts[0])) == 0) { return false; }
for (var i=0; i<parts.length; i++) {
if (parseInt(parseFloat(parts[i])) > 255) { return false; }
}
return true;
} else {
return false;
}
}
If copying code, try to be more selective in what you choose to copy.
That looks as if it was written by an American.
Please don't let your posting agent wrap code lines / annotation.
Please use an informative Subject line.
Line 2 is setting a variable to a RegExp literal and line 3 is using
that to text whether ipaddr is of the form "###.###.###.###", where each
### represents 1-3 decimal digits.
See <URL:http://www.merlyn.demon.co.uk/js-valid.htm> and links for more.
The code can be simplified to this, I think;
function isValidIPAddress(ipaddr) {
var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
if (!re.test(ipaddr)) return false
var parts = ipaddr.split(".")
if (+parts[0] == 0) return false
for (var i=0; i<parts.length; i++) if (+parts[i] > 255) return false
return true }
Your parseInt(parseFloat(x)) is I suppose used to read numbers with
leading zeroes as decimal; the unary + operator will do that, much
faster - see newsgroup FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.