Validation

Phone numbers

Phone numbers checked against Google's libphonenumber data, stored in E.164, shown in international or national form.

Version 1.0.0 · 2 functions · 1 crate

#forms#phone#sms#i18n

Functions

What use phone.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use phone_parse, phone_e164 from phone (Quillo 0.6).

phone_parse(number: str, region: str) → anyor an error

{ e164, international, national, country_code, region, valid } for a number, read as from region ("US", "GB", "PH") when it has no +.

phone_e164(number: str, region: str) → stror an error

The number in E.164 (+14155550123), to store and to send texts to, or an error when it is not a valid number.

Get it

$ quillo install phone               # phone.qwer into this folder$ quillo install phone --example     # and its example, phone.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/phone.qwer$ curl -O https://quillolang.org/plugins/files/phone.qwe

Then, in your program, all of it or only what you call:

main.qwe
use phone.qweruse phone_parse, phone_e164 from phone      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
phonenumber = "0.3"

Example

phone.qwe
// A sign-up form's phone field, stored one way whatever was typed.////   quillo run phone.qwe      then open http://localhost:8080/phone?n=(415)+555-2671&region=USuse "phone.qwer"route GET /phone ? { n: str, region: str = "US" } = phone_parse(n, region)?route GET /store = { ph: phone_e164("0917 123 4567", "PH")?, uk: phone_e164("+44 20 7946 0958", "")?, bad: is_err(phone_e164("123", "US")) }
$ quillo run phone.qwe$ curl 'localhost:8080/phone?n=(415)+555-2671&region=US'$ curl 'localhost:8080/store'

Source

phone.qwer
//! Phone numbers checked and written one way: E.164 for storing, international and national forms for showing, from Google's libphonenumber data.//! version: 1.0.0//!//! [dependencies]//! phonenumber = "0.3"use phonenumber::{country, Mode};use serde_json::{json, Value};fn parse(number: &str, region: &str) -> Result<phonenumber::PhoneNumber, String> {    let id = if region.is_empty() { None } else { Some(region.to_uppercase().parse::<country::Id>().map_err(|_| format!("`{}` is not a region code (US, GB, PH...)", region))?) };    phonenumber::parse(id, number).map_err(|e| e.to_string())}/// `{ e164, international, national, country_code, region, valid }` for a number, read as from `region` ("US", "GB", "PH") when it has no `+`.pub fn phone_parse(number: &str, region: &str) -> Result<Value, String> {    let n = parse(number, region)?;    Ok(json!({        "e164": n.format().mode(Mode::E164).to_string(),        "international": n.format().mode(Mode::International).to_string(),        "national": n.format().mode(Mode::National).to_string(),        "country_code": n.code().value(),        "region": n.country().id().map(|id| format!("{:?}", id)),        "valid": phonenumber::is_valid(&n),    }))}/// The number in E.164 (`+14155550123`), to store and to send texts to, or an error when it is not a valid number.pub fn phone_e164(number: &str, region: &str) -> Result<String, String> {    let n = parse(number, region)?;    if !phonenumber::is_valid(&n) {        return Err(format!("{} is not a valid phone number", number));    }    Ok(n.format().mode(Mode::E164).to_string())}
Thread
711Useful? Like it, or add to its thread below.

Thread 0 posts

On Phone numbers: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.