Skip to content

Instantly share code, notes, and snippets.

@rahman541
Last active November 3, 2021 02:46
Show Gist options
  • Save rahman541/4a7965f070798a5dade0 to your computer and use it in GitHub Desktop.
Save rahman541/4a7965f070798a5dade0 to your computer and use it in GitHub Desktop.
IC Number input validation on server side (PHP) using Regular Expression (Regex)
<?php
$ic = "123456-11-1111"; //default ic if not set
if(isset($_POST["ic"])){
$ic = $_POST["ic"];
}
$regex = '/^[0-9]{6}-[0-9]{2}-[0-9]{4}$/';
if (preg_match($regex, $ic)) {
echo 'Valid';
} else {
echo 'Invalid';
}
echo " IC Number";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>IC Validator</title>
</head>
<body>
<form method="POST">
<input type="text" name="ic" placeholder="IC eg. 990104-07-5555">
<input type="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment