Php postcode validator for partial UK postcode - php

I am working on a project that allows a user to search shops close to their postcode. As the site is now, it verifies via a full postcode only such as M28 3HE. I need to amends this to allow the user to be able to search for partial postcodes such as M28. Looking through the files, this looks to be the validator:
private function is_valid_postcode( $postcode ) {
// Remove all whitespace.
$postcode = preg_replace( '/\s/', '', $postcode );
$postcode = strtoupper( $postcode );
if ( preg_match( '/^[A-Z]{1,2}[0-9]{2}$/', $postcode )
|| preg_match( '/^[A-Z]{1,2}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{2}$/', $postcode )
|| preg_match( '/^GIR0[A-Z]{2}$/', $postcode ) ) {
return true;
} else {
$_SESSION['shop_errors'][] = 'The postcode entered could not be validated.';
return false;
}
}
How can I amend this to allow for partial postcodes?
Thank you

Related

Contact Form 7 Validate Minlength

first of all I'm using latest WordPress and CF7 version. I want to include the minlength validation for the tel field before. I know the syntax minlength=""
can be used inside the CF7, but for unknown reason, it won't work. Only maxlength="" is ok.
I already contacted the plugin support, but seems like no further response. So, I search here and found some code and I edit it so that the field will return an error if user put less than 10 characters. I put the codes inside functions.php
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'Subject'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if($phoneNumber < "9"){
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);
The result now is, it always display the "phone number is less" even though I insert more than 9 characters. May I know what wrong and how to solve it?
as I've tested you must have your tel field [tel* phonenumber tel-503] where phonenumber is name of postfield you are posting, second issue in your code is $name=='Subject' as you are validating tel so $name will be phonenumber. So it will be like this:
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'phonenumber'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if(strlen($phoneNumber) < 9){
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);
Your $phoneNumber is a string. You will need to get the length of the string to compare with 9.
Your code will become:
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'Subject'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if(strlen($phoneNumber) < 9){//<=====check here
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);

Check for mobile number CF7

I have a ContactForm7 form which submits data to a CRM. All works fine but now I need to differentiate between mobile and landline numbers. If a number starts with 07 it will be accepted as a mobile number.
looking at other threads I've tried the following but now neither the mobile or telephone field are being populated in the crm or being passed to the log file?
function process_contact_form_data( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
if ( 'Quote Form_Contact' || 'Quote Form_Product' || 'Quote Form' == $title ) {
$firstName = $posted_data['user_first_name'];
$lastName = $posted_data['user_last_name'];
$email= $posted_data['your-email'];
$phone = $posted_data['your-number'];
$message = $posted_data['your-message'];
$bp = $posted_data['BP'][0];
$phone = $pattern;
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
$match = preg_match($pattern,$phone);
if ($match != false) {$mobile = $phone;} else {$mobile= '';};
}
$error = false;
try
{
$relationshipId = postRelationship($firstName,$lastName,$email,$phone,$bp);
$opportunityId = postOpportunity($relationshipId,$message);
postOpportunityNote($relationshipId,$opportunityId,$message);
// postTask($relationshipId);
}
catch (Exception $e)
{
$error=true;
}
if($error || !isset($relationshipId) || !isset($opportunityId) || $relationshipId <= 0 || $opportunityId <= 0)
{
$log->lfile(ABSPATH . 'quotevine.log');
$log->lwrite('ERROR: With Email Address ' . $email);
$log->lclose();
}
}
add_action( 'wpcf7_before_send_mail', 'process_contact_form_data');
Your are overwriting your $phone variable in this line $phone = $pattern; with an undefined variable which will cause $phone to be NULL.
But after commenting out that line, the value of $mobile will still not be correct because a mobile number starts with 07 and the regex matches both the landline and the mobile number, for example:
07123123123
+447123123123
What you could do is if the match succeeded, check if the string starts with +44 to verify it is a mobile number.
preg_match returns false if an error occurred but I think you want to verify if the match is correct.
$phone = "+447123123123";
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
$mobile= '';
if (preg_match($pattern,$phone) && 0 === strpos($phone, '07')) {
$mobile = $phone;
}
You overwrite the phone with an undefined variable.
// Phone is now a phone number I assume
$phone = $posted_data['your-number'];
$message = $posted_data['your-message'];
$bp = $posted_data['BP'][0];
// $pattern is as far as I can see undefined
// $phone =NULL
$phone = $pattern;
// You set pattern
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
// Here you regex if the pattern matches NULL which it does not.
$match = preg_match($pattern,$phone);

Gravity Forms regex for US ZIP code

I'm trying to set up some form validation for a Gravity Form that I've created. One of the fields that I need to validate is a US ZIP code. I want to pass ZIPs that follow the nnnnn and nnnnn-nnnn patterns. Here's my code:
if ( $field->type == 'address' ) {
$zip = rgar( $value, $field->id . '.5' );
if ( preg_match( "(^(?!0{5})(\d{5})(?!-?0{4})(|-\d{4})?$)", $zip ) && ! $field->get_input_property( '5', 'isHidden' )
) {
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? __( 'Please enter a valid ZIP code (ie. 00000 or 00000-0000).', 'gravityforms' ) : $field->errorMessage;
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
My form continues to fail validation and I can't figure out why. I've double checked that .5 is the correct input field number of the ZIP code. Any suggestions?
My form can be found at http://marcusjones.wpengine.com/
shouldn't be easier to use:
/(^\d{5}$)|(^\d{5}-\d{4}$)/
or other function fe:
function isValidPostalCode(postalCode, countryCode) {
switch (countryCode) {
case "US":
postalCodeRegex = /^([0-9]{5})(?:[-\s]*([0-9]{4}))?$/;
break;
default:
postalCodeRegex = /^(?:[A-Z0-9]+([- ]?[A-Z0-9]+)*)?$/;
}
return postalCodeRegex.test(postalCode);
}
and "if" you'll add quite simple.

Preg Split ignore case

I'm trying to ignore uppercase or lowercase with the code below to detect whether the user is blocked or not. Is working when matching the username or email but with the case problem, the validation does not work. How to make it case insensitive? Thanks for helping.
$msg = "something";
$blocked = preg_split('/[\r\n]([a-z])([A-Z])+/', admin_get_option('blocked_users'), -1, PREG_SPLIT_NO_EMPTY);
if ( isset($form['username_or_email']) && in_array( $form['username_or_email'], $blocked) ) {
$errors['username_or_email'] = $msg;
}
if ( isset($form['user_login']) && in_array( $form['user_login'], $blocked) ) {
$errors['user_login'] = $msg;
}
if ( isset($form['user_email']) && in_array( $form['user_email'], $blocked) ) {
$errors['user_email'] = $msg;
}
" i " Modifier Makes the match case insensitive

how to know if it's email or username inputed

Hello everyone i want to ask question my html form, requires to input username/Email that you can put.
Then it searches by username or email if in database that account exists if yes process.
The script works, but only with email.
My problem is how to identify in input field is the user written an username or email? Now it checks both but for some reason it dosen't detect username only email typed.
function getUserEmailExist( $input )
{
global $database;
if( preg_match( '/^[a-z0-9]+([_\\.-][a-z0-9]+)*#([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i', $input ) ) {
$type = 2;
$get = $database->checkExistRecovery( $input, $type );
}
if( preg_match( '/[^0-9A-Za-z]/', $input ) ) {
$type = 1;
$get = $database->checkExistRecovery( $input, $type );
}
if( $get ) {
$this->updateRecover( $input, $type );
} else {
return false;
}
}
You need to move your email if statement below.
Because if the input is an email, it's going to match the second preg match anyway.
So you're overwriting your $type variable.
Fixed.
function getUserEmailExist( $input )
{
global $database;
if( preg_match( '/[A-Za-z0-9]+/', $input ) ) {
$type = 1;
$get = $database->checkExistRecovery( $input, $type );
}
if( preg_match( '/^[a-z0-9]+([_\\.-][a-z0-9]+)*#([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i', $input ) ) {
$type = 2;
$get = $database->checkExistRecovery( $input, $type );
}
if( $get ) {
$this->updateRecover( $input, $type );
} else {
return false;
}
}
I think you want your second regex to be /[A-Za-z0-9]+/. You are currently looking for anything that doesn't contain those characters.
Why do you care about all that validation logic? I can't see your actual DB queries, but it seems you could easily do something like this:
SELECT * FROM users
WHERE email = ? OR username = ?
Where ? would be the email or username value.

Categories