I came upon it by complete accident when looking at some question that asked for validating IP addresses in JavaScript. The following solution was proposed in an answer:
function ValidateIPaddress(ipaddress)
{
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(myForm.emailAddr.value))
{
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}
It...does stuff. I tried to just copy/paste it in the browser console and see if it works. Imagine my surprise when it didn't. But not because the regex was wrong; it didn't work because it didn't know what myForm.emailAddr.value
was. I scrolled all the way to the right to see that it's indeed checking something called myForm.emailAddr.value
against the regex. But the question most definitely didn't have any such HTML.
So, coupled with the peculiar style with which it was written, it became clear it was not written by the answerer. I decided to look for the original...and there are many:
- Validating emails and applying length check
- got undefined using Grunt.js
- How to validate an email address in JavaScript
- How to validate an email address in JavaScript (no, I didn't copy paste twice by accident)
- How to validate an email address in JavaScript * (nor trice)
- Validate email address textbox using JavaScript *
- Validation with Javascript *
But "WAIT!" I hear you say, "Those are about emails" you exclaim. Yep, they are. They just all use the myForm.emailAddr.value
string. The other common thing between them is that it's the same code. Some of the answers (marked with an asterisk) actually give appropriate credit to the site W3Resources for the code. The others copy pasted it from there or from here. It's not very clear.
But let's get back to the IP address thing:
- Validate an IP Address with Mask in javascript [ regarding address range ]
- JS Validation IP:Port *
- How to validate an ip address with custom format using regular expression *
These last two give a link to W3Resources again, but this time an article on IP addresses. It appears the author(s) of W3Resources itself did a copy/paste error when they've taken the function from their email article. And now we find their frankly not very good code all over the place.
Regardless of the quality of the code in the article, answers that use the code are usually very bad:
- just a code dump
- sometimes doesn't even solve the problem asked for
- even if copy/pasted to the correct question, the code doesn't work unless you modify either it or your other code to accommodate where it reads the data from
- no attribution
The last one is honestly what bothers me the most...although the rest don't help, either. At least some answers have taken the code and explain what it does. That seems fine. The ones that link to the article are also "OK". Not great but at least there is attribution and you can go read the article for explanation.
Should we do anything about these? If so, then what--just downvote and that's it?
Note that I suspect there are a lot more instances of this code. I'll continue searching and update them later.