Question

I am trying to use jquery validationengine to check MAC is validated or not, in validationengine, I add custom attribute called "onlyMAC" like this:

"onlyMAC":{
"regex":"/^([A-Z0-9]){16}$/",
"alertText":"* MAC invalid"}

and in form:

<form id='myform'>
<input name="mac" type="text" value="" class="validate[required,custom[onlyMAC]]"/>
</form>
<script>
$('#myform').validationEngine();
</script>

But whatever I input for mac, the validationEngine always return that alertText: "* MAC invalid". I checked my regex here http://jsfiddle.net/chennet/LRfW6/, it looks fine. So, where I did wrong?

Was it helpful?

Solution

Have a look at the example in the documentation: https://github.com/posabsolute/jQuery-Validation-Engine#selectors

Remove the quotes from your regex definition:

"onlyMAC":{
"regex":/^([A-Z0-9]){16}$/,
"alertText":"* MAC invalid"}

The issue is that you are mixing the regex literal syntax with the regex constructor:

/^([A-Z0-9]){16}$/    // RegExp literal uses the slashes
new RegExp('^([A-Z0-9]){16}$')    // RegExp constructor takes a string (without the slashes).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top