Skip to content

Instantly share code, notes, and snippets.

@allefgomes
Created June 18, 2019 22:32
Show Gist options
  • Save allefgomes/97e9acb5cd2ebf4b6cc2c2f0e98da243 to your computer and use it in GitHub Desktop.
Save allefgomes/97e9acb5cd2ebf4b6cc2c2f0e98da243 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" oninput="Masks.phone();" id="phone_id" />
</body>
<script>
Masks = {
phone_id: 'phone_id',
phone: function() {
var value = document.getElementById(this.phone_id).value;
formatted = value.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3')
.replace(/(-\d{4})\d+?$/, '$1')
document.getElementById(this.phone_id).value = formatted;
}
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment