{"__v":15,"_id":"56a2654ceedde7170038f3d1","category":{"__v":9,"_id":"56b200c0f48f270d00e0de6f","pages":["56b201173b6f690d00bdb68e","56b20ef3bf040b0d00588be4","56b214097ae4550d00062928","56b21502bf040b0d00588bea","56c1ffb1d07cc50d005e5f5e","56c74a05af5bc40d00d6bb5b","56d412b38001e30b00896dd5","56d85fc1c83c5f0b00d288a2","56d866f4a957fb1500765a40"],"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-03T13:29:36.476Z","from_sync":false,"order":1,"slug":"c","title":"C# Quickstarts"},"parentDoc":null,"project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","version":{"__v":20,"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-22T17:22:20.272Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Now let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the (ValidateNumber)[doc:rest-pnv-validatenumber] method on the Phone Number Validation API to do this.\n\nFirst, we'll define the response type for the ValidateNumber method. For this example we only need the NumberStatus property, so we won't include any other properties.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public class ValidateResponse\\n{\\n\\t\\tpublic int NumberStatus { get; set; }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNext we'll add a ValidateNumber wrapper function that returns this status:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    int ValidateNumber(string phoneNumber)\\n    {\\n        var client = new RestClient(\\\"https://services.dynmark.com/webapi/phonenumbervalidation/validatenumber\\\");\\n        client.Authenticator = new HttpBasicAuthenticator(username, password);\\n        client.Timeout = 130000;\\n\\n        var body = new\\n        {\\n            Number = phoneNumber,\\n            IsInternational = true\\n        };\\n\\n        var request = new RestRequest(Method.POST);\\n        request.AddHeader(\\\"Accept\\\", \\\"application/json\\\");\\n        request.AddJsonBody(body);\\n\\n        var response = client.Execute<ValidateResponse>(request);\\n\\n        if (response.StatusCode == HttpStatusCode.OK)\\n        {\\n            return response.Data.NumberStatus;\\n        }\\n        else if (response.StatusCode == HttpStatusCode.BadRequest)\\n        {\\n            throw new ArgumentException(\\\"Bad request format\\\");\\n        }\\n\\n        throw new ArgumentException(\\\"Non success response\\\");\\n    }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nFinally, add code to call the ValidateNumber function and report back the result before sending the message:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    var validateStatus = ValidateNumber(\\\"447700900000\\\");\\n    switch (validateStatus)\\n    {\\n        case 2:\\n            <div>You phone is on, we'll send you a message</div>break;\\n        case 3:\\n            <div>You phone is off, please switch it on and try again in a few minutes</div>break;\\n        case 4:\\n            <div>You phone number appears to be dead.</div>break;\\n        case 5:\\n            <div>You phone number isn't on a mobile network. Have you entered a landline number.</div>break;\\n        case 6:\\n            <div>It looks like you entered something that isn't a phone number.</div>break;\\n    }\\n    \\n    if (validateStatus != 2)\\n    {\\n      \\treturn;\\n    }\\n\\n    ...\\n    \\n    Send Message\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"qs-cs-validate-phone-numbers","type":"basic","title":"Validate Phone Numbers"}

Validate Phone Numbers


Now let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the (ValidateNumber)[doc:rest-pnv-validatenumber] method on the Phone Number Validation API to do this. First, we'll define the response type for the ValidateNumber method. For this example we only need the NumberStatus property, so we won't include any other properties. [block:code] { "codes": [ { "code": "public class ValidateResponse\n{\n\t\tpublic int NumberStatus { get; set; }\n}", "language": "csharp" } ] } [/block] Next we'll add a ValidateNumber wrapper function that returns this status: [block:code] { "codes": [ { "code": " int ValidateNumber(string phoneNumber)\n {\n var client = new RestClient(\"https://services.dynmark.com/webapi/phonenumbervalidation/validatenumber\");\n client.Authenticator = new HttpBasicAuthenticator(username, password);\n client.Timeout = 130000;\n\n var body = new\n {\n Number = phoneNumber,\n IsInternational = true\n };\n\n var request = new RestRequest(Method.POST);\n request.AddHeader(\"Accept\", \"application/json\");\n request.AddJsonBody(body);\n\n var response = client.Execute<ValidateResponse>(request);\n\n if (response.StatusCode == HttpStatusCode.OK)\n {\n return response.Data.NumberStatus;\n }\n else if (response.StatusCode == HttpStatusCode.BadRequest)\n {\n throw new ArgumentException(\"Bad request format\");\n }\n\n throw new ArgumentException(\"Non success response\");\n }", "language": "csharp" } ] } [/block] Finally, add code to call the ValidateNumber function and report back the result before sending the message: [block:code] { "codes": [ { "code": " var validateStatus = ValidateNumber(\"447700900000\");\n switch (validateStatus)\n {\n case 2:\n <div>You phone is on, we'll send you a message</div>break;\n case 3:\n <div>You phone is off, please switch it on and try again in a few minutes</div>break;\n case 4:\n <div>You phone number appears to be dead.</div>break;\n case 5:\n <div>You phone number isn't on a mobile network. Have you entered a landline number.</div>break;\n case 6:\n <div>It looks like you entered something that isn't a phone number.</div>break;\n }\n \n if (validateStatus != 2)\n {\n \treturn;\n }\n\n ...\n \n Send Message", "language": "csharp" } ] } [/block]