Skip to content

Instantly share code, notes, and snippets.

@shane-reaume
Created November 2, 2012 23:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shane-reaume/4004929 to your computer and use it in GitHub Desktop.
Save shane-reaume/4004929 to your computer and use it in GitHub Desktop.
Server-side(PHP) validation of phone number that also prevents multiple of same digit, i.e. all 1's
function phonenumber($value)
{
$areacode = '[2-9]{1}\d{1}\d{1}'; // You might want to specify '2\d\d' (200 to 299)
$prefix = '[2-9]{1}\d{1}\d{1}';
$regex = '#^(\('.$areacode.'\)|'.$areacode.')[\s\.-]?'.$prefix.'[\.-]?\d{4}$#';
if (preg_match($regex, $value))
{
// Number now is in a suitable format
// extract digits -- remove this section to not test repeated pattern
$digits = preg_replace('#[^\d]+#', '', $value);
// All numbers equal are rejected
if (preg_match('#^(\d)\1{9}$#', $digits))
return false;
// end of pattern check
// Otherwise it is accepted
return true;
}
return false; // Not in a recognized format
}
@shane-reaume
Copy link
Author

This is an update to RSFormPro phone validation to be specific to US phone numbers only. I use this as Server-Side validation, along with jQuery validation and a phone mask so user can only input (2xx)2xx-xxxx

@shane-reaume
Copy link
Author

Path is typically site\components\com_rsform\helpers\validation.php

@shane-reaume
Copy link
Author

Also, can add CSS...
text-transform: uppercase;
JavaScript...
function toUpCase()
{
document.getElementById("first").value=document.getElementById("first").value.toUpperCase();
document.getElementById("last").value=document.getElementById("last").value.toUpperCase();
document.getElementById("email").value=document.getElementById("email").value.toUpperCase();
}

@shane-reaume
Copy link
Author

To filter phone input more on mask, in the <script src="/media/system/js/jquery.maskedinput-1.3.js" type="text/javascript"></script> file, you can append to the mask 2:"[2-9]"

...then change the jQuery to $('#phone').mask('(299)299-9999');

This will limit the start of the prefix and leading number to slow random number inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment