AzLearn

تمرين إعادة البناء

String Validator

أعد كتابة مدقّق نصوص TypeScript للبريد والرابط ورقم الهاتف.

typescript ~12 دقيقة مبتدئ
أعد بناء الكود Rebuild

هذا هو الكود. اكتبه بنفسك.

الكود المرجعي
type Validator = (value: string) => boolean;

const isEmail: Validator = (value) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
const isUrl: Validator = (value) => /^https?:\/\/[^\s/$.?#].[^\s]*$/.test(value);
const isPhone: Validator = (value) => /^\+?[0-9\s-]{7,15}$/.test(value);

type ValidationResult = {
  value: string;
  email: boolean;
  url: boolean;
  phone: boolean;
};

function validate(value: string): ValidationResult {
  return {
    value,
    email: isEmail(value),
    url: isUrl(value),
    phone: isPhone(value),
  };
}

const samples = ["[email protected]", "https://learn.azizwares.sa", "+966 555 123 456"];

for (const sample of samples) {
  console.log(validate(sample));
}
اكتب هنا