CodeSOD: An Alerting Validation

This post was originally published on this site

The Daily WTF

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){

To read the full article click on the 'post' link at the top.