Sign in to follow this  
Lucidquiet

[web] Form validation REs

Recommended Posts

Lucidquiet    199
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-

Share this post


Link to post
Share on other sites
Sander    1332
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.

Share this post


Link to post
Share on other sites
Mathachew    352
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 javascript 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this