Jump to content
  • Advertisement
Sign in to follow this  
Lucidquiet

[web] Form validation REs

This topic is 4056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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.

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!