c# - Telephone number validation -
i have code validate phone number looks bit awkward. i'm guessing theres better way go this. how can make more efficient?
public static bool validtelephoneno(string telno) { bool condition = false; while (condition == false) { console.writeline("enter phone number."); telno = console.readline(); if (telno.length > 8) { if (telno.startswith("+") == true) { char[] arr = telno.tochararray(); (int = 1; < telno.length; a++) { int temp; try { temp = arr[a]; } catch { break; } if (a == telno.length - 1) { condition = true; } } } } } return true; }
your goal can achieved using regular expressions:
public static bool validtelephoneno(string telno) { return regex.match(telno, @"^\+\d{1,7}$").success; }
this pattern stated: consists of integers, less 8 digits in length , has plus @ start, , pattern can modified if conditions somehow more complex.
Comments
Post a Comment