[web] Form validation REs

Started by
2 comments, last by Mathachew 16 years, 9 months ago
Hello: I looked around for some ready to use REs for the oft used form fields, and I haven't found much. I was wonderring if someone knows of a set of form fields validation REs they would recommend? L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Advertisement
Even for often used fields it depends on the underlying structure/backend exactly what is allowed in a form field or not. Take usernames for example. If you have a normal website there is no reason why spaces and special characters should not be allowed. But if you attach it to webmail then the username needs to meet RFC 2822. You create *nix accounts? Better limit it further. Giving them their own profile? Then better stick to letters that can be used in URLs. Etcetera, etcetera, etcetera.

That said, the comment sections of the POSIX and PCRE Regular Expressions manual pages from the PHP manual are a rich source of regular expressions.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Hi,

I usually get mine from here. If I can't find what i'm looking for it is usually a good source of inspiration:

http://regexlib.com/DisplayPatterns.aspx
I wrote a plugin for jQuery that filters text in text boxes, however, it's tied directly into the autotabbing feature. It allows you to specify what kind of characters are allowed in the text box: special, alpha, numeric, alphanumeric and all. Since it's built on jQuery, I had to modify the code so that it would work as a standalone. This is a &#106avascript method of validating the field data. JS can be disabled (though I have yet to come across one person that does), so this method is not 100% reliable. This may not be exactly what you're looking for, but it seems like it's a start for what you need. You'll probably want to create a case for each type of validation, so that you remove whatever you need if the field is a username, etc.

Create a JS function to pass your text when validating your fields. Include an extra parameter to specify what kind of formatting needs to be done:

<script type="text/javascript">// Call: onkeyup="text_filter(this, 'alpha');"function text_filter(e, format){	var txt = e.value;	switch(format)	{		case 'special':			var pattern = new RegExp('[0-9]+', 'g');			var txt = txt.replace(pattern, '');			break;		case 'alpha':			var pattern = new RegExp('[^a-zA-Z]+', 'g');			var txt = txt.replace(pattern, '');			break;		case 'numeric':			var pattern = new RegExp('[^0-9]+', 'g');			var txt = txt.replace(pattern, '');			break;		case 'alphanumeric':			var pattern = new RegExp('[^0-9a-zA-Z]+', 'g');			var txt = txt.replace(pattern, '');			break;		case 'all':		default:			break;	}	e.value = txt;}</script><input type="text" name="test" onblur="text_filter(this, 'numeric');" value="" />


Hopefully this plus the links above will help guide you in the right direction.

This topic is closed to new replies.

Advertisement