Feed aggregator
CodeSOD: An Alerting Validation
There are things which are true. Regular expressions frequently perform badly. They're hard to read. Email addresses are not actually regular languages, and thus can't truly be validated (in all they're many possible forms) by a pure regex.
These are true. It's also true that a simple regex can get you most of the way there.
Lucas found this in their codebase, for validating emails.
function echeck(str) { var at="@"; var dot="."; var lat=str.indexOf(at); var lstr=str.length; var ldot=str.indexOf(dot); if (str.indexOf(at)==-1){ alert("You must include an accurate email address for a response."); return false; } if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ alert("You must include an accurate email address for a response."); return false; } if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ alert("You must include an accurate email address for a response."); return false; } if (str.indexOf(at,(lat+1))!=-1){ alert("You must include an accurate email address for a response."); return false; } if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ alert("You must include an accurate email address for a response."); return false; } if (str.indexOf(dot,(lat+2))==-1){ alert("You must include an accurate email address for a response."); return false; } if (str.indexOf(" ")!=-1){ alert("You must include an accurate email address for a response."); return false; } return true; }It checks that the string contains an "@", and the "@" is not at the beginning or end of the string. Then it does the same check for a ".". Then it checks that there isn't a second "@". Then it checks that there are at least two non-"@" characters before the ".". Then it checks that there's at least one "." after the "@". Then it checks that there are no spaces.
Like a regex, I don't think this covers the entire space of valid and invalid email addresses, but that's just because the email address spec is complicated. It likely qualifies as "good enough", on that front. But it's the most awkward way to express that series of tests, especially since they create variables which might be useful, but never use them, thus calling str.indexOf many, many times. The awkwardness becomes more obvious with the way it outputs the same error message in multiple branches. Outputs them using alert I might add, which is the kind of choice that should send someone to the Special Hell™.
[Advertisement] Plan Your .NET 9 Migration with ConfidenceYour journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. Download Free Guide Now!
Blender-Rendered Movie 'Flow' Wins Oscar for Best Animated Feature, Beating Pixar
Read more of this story at Slashdot.
Linux's Marketshare Drops in Monthly Steam Survey
Read more of this story at Slashdot.
Lenovo Teases Solar-Powered and Foldable-Screen Laptops in Latest Concepts
Read more of this story at Slashdot.
Trump Names Cryptocurrencies for 'Digital Asset Stockpile' in Social Media Post
Read more of this story at Slashdot.
'Exponential Spin-up' In Geothermal Energy Projects Brings Hope for Green Power
Read more of this story at Slashdot.
How Buildings Are Staying Cool and Saving Money - with Batteries Made of Ice
Read more of this story at Slashdot.
What Happened When Conspiracy Theorists Talked to OpenAI's GPT-4 Turbo?
Read more of this story at Slashdot.
Ask Slashdot: Would You Accept a Free Ride Into Space?
Read more of this story at Slashdot.
Fast New 3D Printing Technique Shines Holograms into Resin
Read more of this story at Slashdot.
First Petawatt Electron Beam Arrives, Ready To Rip Apart Matter and Space
Read more of this story at Slashdot.
Malicious PyPI Package Exploited Deezer's API, Orchestrates a Distributed Piracy Operation
Read more of this story at Slashdot.
Watch 'Blue Ghost' Attempt Its Landing on the Moon
Read more of this story at Slashdot.
27-Year-Old EXE Became Python In Minutes. Is AI-Assisted Reverse Engineering Next?
Read more of this story at Slashdot.
Utah Could Become America's First State To Ban Fluoride In Public Water
Read more of this story at Slashdot.
Microsoft Outage Leaves Tens of Thousands Unable to Access Email and Other Apps
Read more of this story at Slashdot.
AMD Reveals RDNA 4 GPU Architecture Powering Next Gen Radeon RX 9070 Cards
Read more of this story at Slashdot.
Mozilla Revises Firefox's Terms of Use, Clarifies That They Don't Own Your Data
Read more of this story at Slashdot.
New Research Suggests Ancient Ocean on Mars
Read more of this story at Slashdot.
Perl's CPAN Security Group is Now a CNA, Can Assign CVEs
Read more of this story at Slashdot.