Skip to content

Instantly share code, notes, and snippets.

@cpv123
Last active August 26, 2020 14:13
Show Gist options
  • Save cpv123/45c117400d620ace29b527390df870b6 to your computer and use it in GitHub Desktop.
Save cpv123/45c117400d620ace29b527390df870b6 to your computer and use it in GitHub Desktop.
import { Form, Input } from 'antd';
...
const validatePhoneNumber = (rule, value, callback) => {
const phoneNumberPattern = /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/;
const isValid = phoneNumberPattern.test(value);
if (!isValid) {
return callback('Not a valid phone number');
}
return callback();
}
...
<Form.Item
name="phoneNumber"
label="Phone number" >
rules: [
{
required: true,
message: 'You must enter a phone number!',
},
{ validator: this.validatePhoneNumber },
],
>
<Input placeholder="Enter a phone number" />
</Form.Item>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment