Tag Archive for validate

Validate North America Phone Number

function fnValidatePhone($mValue)
{
$sPattern = '/^[(]?[0-9]{3}[)]?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/';
return preg_match($sPattern, $mValue);
}

source

Validate Canadian Postal Code

function fnValidatePostal($mValue, $sRegion = '')
{
$mValue = strtolower($mValue));
$sFirst = substr($mValue, 0, 1);
$sRegion = strtolower($sRegion);

$aRegion = array(
'nl' => 'a',
'ns' => 'b',
'pe' => 'c',
'nb' => 'e',
'qc' => array('g', 'h', 'j'),
'on' => array('k', 'l', 'm', 'n', 'p'),
'mb' => 'r',
'sk' => 's',
'ab' => 't',
'bc' => 'v',
'nt' => 'x',
'nu' => 'x',
'yt' => 'y'
);

if (preg_match('/[abceghjlkmnprstvxy]/', $sFirst) && !preg_match('/[dfioqu]/', $mValue) && preg_match('/^wdw[- ]?dwd$/', $mValue))
{
if (!empty($sRegion) && array_key_exists($sRegion, $aRegion))
{
if (is_array($aRegion[$sRegion]) && in_array($sFirst, $aRegion[$sRegion]))
{
return true;
}
else if (is_string($aRegion[$sRegion]) && $sFirst == $aRegion[$sRegion])
{
return true;
}
}
else if (empty($sRegion))
{
return true;
}
}

return false;
}

source

Validate Credit Card Number

function fnValidateCc($mValue, $sCard = '')
{
if ($sCard == 'visa')
{
$sPattern = '/^4([0-9]{12}|[0-9]{15})$/';
}
else if ($sCard == 'amex')
{
$sPattern = '/^3[4|7][0-9]{13}$/';
}
else if ($sCard == 'mastercard')
{
$sPattern = '/^5[1-5][0-9]{14}$/';
}
else if ($sCard == 'discover')
{
$sPattern = '/^6011[0-9]{12}$/';
}
else if ($sCard == 'dinners')
{
$sPattern = '/^[30|36|38]{2}[0-9]{12}$/';
}
else if (empty($sCard))
{
$sPattern = '/^[0-9]{13,19}$/';
}
else
{
return false;
}

if (preg_match($sPattern, $mValue))
{
// Modulo 10
$iSum = 0;
$iWeight = 2;
$iLength = strlen($mValue);

for ($i = $iLength - 2; $i >= 0; $i--)
{
$iDigit = $iWeight * $mValue[$i];
$iSum += floor($iDigit / 10) + $iDigit % 10;
$iWeight = $iWeight % 2 + 1;
}

if ((10 - $iSum % 10) % 10 == $mValue[$iLength - 1])
{
return true;
}
}

return false;
}

source

Validate Email

function fnValidateEmail($mValue)
{
return filter_var($mValue, FILTER_VALIDATE_EMAIL);
}

source

Validate Url

function fnValidateUrl($mValue)
{
return filter_var($mValue, FILTER_VALIDATE_URL);
}

source

validate save code for importing csv file

					// $error = false;

// validate the row
// $this->set($data);
// if (!$this->Member->validates())
// {
// 	$this->_flash(__(sprintf('Post for Row %d failed to validate.',$i), true),'warning');
// 	$error = true;
// }

// save the row
// if (!$error && !$this->Member->save($data))
// {
// 	$this->Member->_flash(__(sprintf('Post for Row %d failed to save.',$i), true),'warning');
// }

// save the row
// if (!$error)
// {
// 	// for more info on _flash() please visit:
// 	// <a href="http://mrphp.com.au/node/1820" >http://mrphp.com.au/node/1820</a>
// 	$this->Member->_flash(__(sprintf('Post for Row %d was saved.',$i), true),'success');
// }

source

isDateValid – Validate a date in javascript

/**
* Check if a date is valid
* @return bool
* @author Mardix
* @since: June 30 2009
*/
isDateValid = function(year,month,date){

if(year && month && date){
if(month==2){
if((year%4 == 0) && date>29)
return false
else if((year%4!=0) && date > 28)
return false;
}

if((month!=2 && month!=8) && ((month%2 != 1) && date>30))
return false;
else
return true;
}

else
return false;
}

source

Validate e-mail

function is_email($mail) {
return (preg_match("/[-a-zA-Z0-9_.+]+@[a-zA-Z0-9-]{2,}.[a-zA-Z]{2,}/", $mail) > 0) ? true : false;
}

source

TextMate Local CSS Validation

#!/usr/bin/env ruby -wKU

scope = STDIN.read
scope.gsub!(/< /?style.*?>/, '')

open('|curl -sF file=@-;type=text/css -F lang=en <a href="http://jigsaw.w3.org/css-validator/validator" >http://jigsaw.w3.org/css-validator/validator</a>', 'r+') do |io|
io < < scope
io.close_write

while io.gets
$_.gsub!(/</title>/, '&')
print $_
end
end

source

confirm password background change

<html>
<head>
<title>password confirm</title>
<script type="text/javascript">
function validateConfPass(e, confPassword, pass) {
keychar = getCharacterPressed(e);
if (keychar != "-1") {
if (keychar == "backspace") {
lenOfConf = confPassword.value.length - 1;
if (confPassword.value.substr(0, lenOfConf) != pass.value.substr(0, lenOfConf))
confPassword.style.background = "#FFBBBB";
else
confPassword.style.background = "#FFFFFF";
}
else {
lenOfConf = confPassword.value.length + 1;
if (confPassword.value + keychar != pass.value.substr(0, lenOfConf))
confPassword.style.background = "#FFBBBB";
else
confPassword.style.background = "#FFFFFF";
}
}
}

function getCharacterPressed(e) {
code = getKeyCode(e);
keychar = "-1";
if (pressedPrintableChar(code))
keychar = String.fromCharCode(code);
if (code == <img src='http://www.snippetsmania.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> keychar = "backspace";
return keychar;
}

function getKeyCode(e) {
code = -1;
if (window.event)
code = e.keyCode;
else if (e.which)
code = e.which;
return code;
}

function pressedPrintableChar(code) {
if (code != 8 && code != 13 && code + "" != "undefined")
return true;
return false;
}
</script>
</head>
<body onload="javascript: document.frm.password.focus();">
this code highlights the confirm password field when it is different from the password field<br />
<form name="frm">
password: <input type="password" name="password" id="password*" /><br />
confirm password: <input type="password" id="confPass" onkeypress="javascript: validateConfPass(event, this, document.getElementById('password*'));" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>

source