Monday, March 10, 2008

Regular Expression Validator

This is a simple application of the Javascript Regular Expression matcher.
Digg it!


RegExp
String



Try some regular expressions:

Here you find the code. If you need an explanation, comment below!

<script language="javascript">
function regexMatch()
{
var isIE = (window.ActiveXObject)?true:false;
var attributeClass = (isIE)?"className":"class";

var t1 = document.getElementById("regexField");
var t2 = document.getElementById("string");
var strPattern = "^"+t1.value+"$";
var oTest = t2.value;

var oREGEXP = new RegExp(strPattern);
if (oREGEXP.test(oTest))
{
t2.setAttribute(attributeClass,"right");
}
else
{
t2.setAttribute(attributeClass,"wrong");
}
}
</script>

<style>
.right{background-color:#33FF33;}
.wrong{background-color:#FF5555;}
</style>


<form name="formRegEx" onSubmit="javascript:regexMatch(); return false;">
<input type="text" id="regexField"/>
<input type="text" id="string" />
<input type="button" onClick="javascript:regexMatch();" value="check"/>
</form>


3 comments:

Unknown said...

You just solved the problems of half-humanity.

Anonymous said...

For this kind of stuff, I would prefer the desktop version. When will the VB.NET or Delphi version becomes available? :D

Unknown said...

Hi Average,

This is intented for web client-side validation.