• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't think of an elegant way to do this.. (EMail or URL input - Form)

I currently have a form field on ignitedjobs.com (How to Apply) that only takes a URL because the field value is required|prep_url

Not conducive to handling E-Mail addresses, obviously.

I can't seem to think of an elegant way to test for URL or email content.

As much as I would like to avoid a regular express, it may be the only way.

Is a Regex the only way to go?

Seems like this method is going to add yet another level to my conditional checks (The validation for the Post a Job page is 5 conditionals deep already).

[eluser]Colin Williams[/eluser]
Quote:Is a Regex the only way to go?

Don't fear the regex. It might be better than getting uber-creative with standard PHP functions. If you're worried about resources, just do it and run benchmarks on it. THEN decide if it's too heavy. I've never had so many preg_ functions in my code that it crippled a site (3 million page views, 800k+ uniques, in one day, if you're wondering how big).

if (strpos('http://', $str) !== 0 and strpos('https://', $str) !== 0 and strpos('@', $str) !== FALSE)
  // It must be an email
  // It looks like a url

Is that ironclad? No. Do you need to protect against every single edge-case? Probably not.

Quote:The validation for the Post a Job page is 5 conditionals deep already

Is that the most evil thing ever? Not if it's the only logical way to run the checks. I like to get things working first, then come back and refactor. No sense it letting perfect destroy good. Just comment the code with a todo:

// TODO: Consider refactoring this mess!

Good point.

And I've gone back to the validation a couple of times - I think I've squished it about as well as I can. It was around 9 conditionals, but that was me being intentionally verbose.

The site itself seems to handle about 450req/sec with testing using Siege w/o any caching. Apache Benchmark says ~30 - but AB is notoriously inaccurate.

I suppose it's time to dig into regex and decide how I'm going to do it. May just have to do two tests.

[eluser]Colin Williams[/eluser]
Check back above. I think strpos checks could get you far enough.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.