Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

validation for a uk number?

Featured Replies

i have found a script online that validates a form, the thing is i want it to validate a uk phone number and not the one it currently has, i have tried googling it but to no success! i was wondering if you lot could help me?

 

what would be the code for a uk number?

 

it currently has;

if ( empty($_REQUEST['tele']) ) {
	$pass = 1;
	$alert .= "<li>" . $emptytele . "</li>";
} elseif ( !ereg( "\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}", $_REQUEST['tele'] ) ) {
	$pass = 1;
	$alert .= "<li>" . $alerttele . "</li>";
}

 

thanks guys for your help.

Change the expression within the "ereg" function to:

 

^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

So it should read:

 

if (empty($_REQUEST['tele'])) {
$pass = 1;
$alert .= "<li>" . $emptytele . "</li>";
}
elseif (!ereg( "^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$",$_REQUEST['tele'])) {
$pass = 1;
$alert .= "<li>" . $alerttele . "</li>";
}

The regular expression above allows the following variations of a UK telephone number:

 

  • 01702 123 456
  • 01702123456
  • +44 1702 123 456
  • +44 1702123456

Regards,

Dan

  • Author

Thanks for that Dan, but I just implemented it into my code and it doesn't seem to work, i don't know what's wrong, is it the code or is it something else?

 

Cheers,

Chris

 

Change the expression within the "ereg" function to:

 

^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

So it should read:

 

if (empty($_REQUEST['tele'])) {
$pass = 1;
$alert .= "<li>" . $emptytele . "</li>";
}
elseif (!ereg( "^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$",$_REQUEST['tele'])) {
$pass = 1;
$alert .= "<li>" . $alerttele . "</li>";
}

The regular expression above allows the following variations of a UK telephone number:

 

  • 01702 123 456
  • 01702123456
  • +44 1702 123 456
  • +44 1702123456

Regards,

Dan

What telephone number have you used to test it. All the UK telephone numbers I can think of work with that regular expression (i used http://regexpal.com/ to test).

 

Could you post the rest of your code and a link to your site (if possible).

  • Author

it must be my code then, here is the code...

 

sendmail.php

<?php
/*

This script was created by Bryan Helmig at midmodesign.com. It is licensed under http://creativecommons.org/licenses/by-nc-sa/3.0/us/. 
1)   A quick primer: place this on your server. Create a form like below:
--------------------------------------------------------------------------------------------------------
<form action="sendmail.php" method="post" id="contactForm">
<p>Name:</p> <input type="text" name="name" value="" id="name" />
<p>Email:</p> <input type="text" name="email" value="" id="email" />
<p>Telephone:</p> <input type="text" name="tele" value="" id="tele" />
<span style="display:none;"><p>Honeypot:</p> <input type="text" name="last" value="" id="last" /></span>
<p>Message:</p> <textarea rows="5" name="message"></textarea>
<input type="submit" value="Send Message" />
</form
--------------------------------------------------------------------------------------------------------
2)   This will work fine for a standard form. If you want ajax power, add this div above or below and hide it with css.
--------------------------------------------------------------------------------------------------------
<div class="message"><div id="alert"></div></div>
--------------------------------------------------------------------------------------------------------
3)   And add this to the head: Also download jquery-latest.pack.js and jquery.form.js and point to those appropriately.
--------------------------------------------------------------------------------------------------------
<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() { 
var options = { 
target:        '#alert',
beforeSubmit:  showRequest,
success:       showResponse
}; 
$('#contactForm').ajaxForm(options); 
}); 
function showRequest(formData, jqForm, options) { 
var queryString = $.param(formData); 
return true; 
} 
function showResponse(responseText, statusText)  {  
} 
$.fn.clearForm = function() {
 return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
  return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
  this.value = '';
else if (type == 'checkbox' || type == 'radio')
  this.checked = false;
else if (tag == 'select')
  this.selectedIndex = -1;
 });
};
</script>
--------------------------------------------------------------------------------------------------------

Boom. There it is. 
*/

//        Who you want to recieve the emails from the form. (Hint: generally you.)
$sendto = 'youremail@example.com';

//        The subject you'll see in your inbox
$subject = 'Contact from contact form';

//        Message for the user when he/she doesn't fill in the form correctly.
$errormessage = 'Oops! There seems to have been a problem. May we suggest...';

//        Message for the user when he/she fills in the form correctly.
$thanks = "Thanks for the email! We'll get back to you as soon as possible!";

//        Message for the bot when it fills in in at all.
$honeypot = "You filled in the honeypot! If you're human, try again!";

//        Various messages displayed when the fields are empty.
$emptyname =  'Entering your name? <br />';
$emptyemail = 'Entering your email address?<br />';
$emptytele = 'Entering your telephone number?<br />';
$emptymessage = 'Entering a message?<br />';

//       Various messages displayed when the fields are incorrectly formatted.
$alertname =  'Entering your name using only the standard alphabet?';
$alertemail = 'Entering your email in this format: <i>name@example.com</i>?';
$alerttele = 'Entering your telephone number in this format: <i>555-555-5555</i>?';
$alertmessage = "Making sure you aren't using any parenthesis or other escaping characters in the message? Most URLS are fine though!";

// --------------------------- Thats it! don't mess with below unless you are really smart! ---------------------------------

//Setting used variables.
$alert = '';
$pass = 0;

// Sanitizing the data, kind of done via error messages first. Twice is better!
function clean_var($variable) {
   $variable = strip_tags(stripslashes(trim(rtrim($variable))));
 return $variable;
}

//The first if for honeypot.
if ( empty($_REQUEST['last']) ) {

// A bunch of if's for all the fields and the error messages.
if ( empty($_REQUEST['name']) ) {
	$pass = 1;
	$alert .= "<li>" . $emptyname . "</li>";
} elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] ) ) {
	$pass = 1;
	$alert .= "<li>" . $alertname . "</li>";
}
if ( empty($_REQUEST['email']) ) {
	$pass = 1;
	$alert .= "<li>" . $emptyemail . "</li>";
} elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_REQUEST['email']) ) {
	$pass = 1;
	$alert .= "<li>" . $alertemail . "</li>";
}
if (empty($_REQUEST['tele'])) {
       $pass = 1;
       $alert .= "<li>" . $emptytele . "</li>";
}elseif (!ereg( "^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$",$_REQUEST['tele'])) {
       $pass = 1;
       $alert .= "<li>" . $alerttele . "</li>";
}
if ( empty($_REQUEST['message']) ) {
	$pass = 1;
	$alert .= "<li>" . $emptymessage . "</li>";
} elseif ( ereg( "[][{}()*+?\\^$|]", $_REQUEST['message'] ) ) {
	$pass = 1;
	$alert .= "<li>" . $alertmessage . "</li>";
}

//If the user err'd, print the error messages.
if ( $pass==1 ) {

	//This first line is for ajax/javascript, comment it or delete it if this isn't your cup o' tea.
echo "<script>$(\".message\").hide(\"slow\").show(\"slow\"); </script>";
echo "<b>" . $errormessage . "</b>";
echo "<ul>";
echo $alert;
echo "</ul>";

// If the user didn't err and there is in fact a message, time to email it.
} elseif (isset($_REQUEST['message'])) {

	//Construct the message.
    $message = "From: " . clean_var($_REQUEST['name']) . "\n";
	$message .= "Email: " . clean_var($_REQUEST['email']) . "\n";
    $message .= "Telephone: " . clean_var($_REQUEST['tele']) . "\n";
    $message .= "Message: \n" . clean_var($_REQUEST['message']);
    $header = 'From:'. clean_var($_REQUEST['email']);

//Mail the message - for production
	mail($sendto, $subject, $message, $header);
//This is for javascript, 
	echo "<script>$(\".message\").hide(\"slow\").show(\"slow\").animate({opacity: 1.0}, 4000).hide(\"slow\"); $(':input').clearForm() </script>";
	echo $thanks;

	die();

//Echo the email message - for development
	//echo "<br/><br/>" . $message;

}

//If honeypot is filled, trigger the message that bot likely won't see.
} else {
echo "<script>$(\".message\").hide(\"slow\").show(\"slow\"); </script>";
echo $honeypot;
}
?>

 

contact.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>The Big Party Co.</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="style.css" type="text/css" />

<script type="text/javascript" src="jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() { 
var options = { 
target:        '#alert',
}; 
$('#contactForm').ajaxForm(options); 
}); 

$.fn.clearForm = function() {
 return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
  return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
  this.value = '';
else if (type == 'checkbox' || type == 'radio')
  this.checked = false;
else if (tag == 'select')
  this.selectedIndex = -1;
 });
};

</script>

</head>
<body id="contact">	

<div id="wrapper">

	<div id="header">
		<img src="banner4.gif" alt="" class="banner"/>
	</div><!-- Close Header -->


	<div id="nav">
		<?php include "inc/nav.php"; ?>


	</div><!-- Close Sidebar -->

	<div id="content">
		<h1>Contact</h1>
			<div id="contact1">


				<p>
<div class="message"><div id="alert"></div></div>

<div class="contact">
<form action="sendmail.php" method="post" id="contactForm">
<ul>
<li class="contact">
<label for="name">Name:</label>
<input type="text" name="name" value="" id="name" />
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" value="" id="email" />
</li>
<li>
<label for="tele">Telephone:</label>
<input type="text" name="tele" value="" id="tele" />
</li>
<li class="special">
<label for="last">Don't fill this in:</label>
<input type="text" name="last" value="" id="last" />
</li>
<li>
<label for="message">Message:</label><textarea rows="5" name="message"></textarea>
</li>
<li class="submitbutton">
<input type="submit" value="Send Message" />
</li>
</ul>
</form>
				</p>

	</div>

		</div>	
			</div>

	<div class="clearfix"></div>

</div><!-- Close Wrapper -->


<div id="footer">
	<p>Chris Heirani</p>

</div><!-- Close Footer -->

</body>
</html>

 

A link to the site is here

 

ignore the error message that you get, i just havent changed it.

 

i copied one of the valid numbers you suggested and it didn't work so i guess its something to do with my code.

 

I appreciate your help,

 

Chris

 

What telephone number have you used to test it. All the UK telephone numbers I can think of work with that regular expression (i used http://regexpal.com/ to test).

 

Could you post the rest of your code and a link to your site (if possible).

It seems to work fine, it just doesn't like the spaces for some reason. It can most likely be ammended to accept spaces but you may wish to just put in the error message "dont use spaces".

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.