Combine function for 10 digit phone number - no spaces

special.k45 picture special.k45 · Nov 2, 2012 · Viewed 8.4k times · Source

Been trying to validate with a 10 digit phone number, no spaces or any other characters other than numbers. I think I have the correct reg ex but am unsure how to implement the code so it works with NO spaces too.

What I've done so far: Added in jquery.validationEngine-en.js

// ADDED for 10 digit phone
"phone10": {
  "regex": /^[\d ]+$/, 
  // alertText brings up two error messages so using alertText2
  "alertText2": " numbers only: xxxxxxxxxx", 
},

I then added the phone10 case statement in jquery.validationEngine.js:

case "phone10":
  errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, 
    options, methods._phone10Size);
break;

And put the function below called _phone10Size:

_phone10Size: function(field, rules, i, options) {
  var max = rules[i + 1];
  var len = field.val().length;

  if (len < 10) {
    var rule = options.allrules.phone10;
    return 10 + rule.alertText2;
  }
},

Oh yeah, I've also had to put maxlength="10" on the form input field. I know it's not elegant or even "proper". Can someone tell me how to implement this 10 digits only that doesn't allow spaces? Thank you.

Answer

special.k45 picture special.k45 · Nov 9, 2012

Figured out a solution for 10 numbers only (no spaces). For my class on the field I've put:

class="validate[required,custom[onlyNumber],minSize[10],maxSize[10]] text-input"

and added the function OnlyNumber to line 97 in the jquery.validationEngine-en.js file:

"onlyNumber": {
    "regex": /^[0-9]+$/,
    "alertText": "* Numbers only"
 },