I wanted to write a regular expression using the ASP.Net RegExp validator that would ensure a field contains only numeric and decimal values, and at least one character.
^[0-9]{1,40}(\.[0-9]{1,2})?$
Essentially: [0-9]{1,40}
- meaning at least one to 40 numeric characters.
The ASP.Net regexp validator does not fire for an empty field - where there is not at least one character.
Work-around: using the custom validator with the regexp check in Javascript:
function validateMinTrans(sender, args) {
var error = true;
var txtMinTrans = document.getElementById('TxtMinTrans');
var regexp = new RegExp("^[0-9]{1,40}(\.[0-9]{1,2})?$");
if (txtMinTrans.value.match(regexp)) {
alert("good");
}
else {
alert("bad");
}
if (error)
args.IsValid = false;
else
args.IsValid = true;
}
Thus, I don't even have to check txtMinTrans.length == 0
.
Wondering if anyone else has experienced this.