
Practice 50 JavaScript Validation with Regular expression Programming Questions
Q1. Write a JavaScript program to validate a valid email address using a regular expression.
Input: "user@example.com"
Expected Output: true
Q2. Write a JavaScript program to validate a phone number (format: 123-456-7890) using a regular expression.
Input: "123-456-7890"
Expected Output: true
Q3. Write a JavaScript program to validate a URL using a regular expression.
Input: "https://www.example.com"
Expected Output: true
Q4. Write a JavaScript program to check if a given string contains only numbers using a regular expression.
Input: "1234567890"
Expected Output: true
Q5. Write a JavaScript program to validate if a string contains only letters (both lowercase and uppercase) using a regular expression.
Input: "abcXYZ"
Expected Output: true
Q6. Write a JavaScript program to validate if a string contains only alphanumeric characters using a regular expression.
Input: "abc123XYZ"
Expected Output: true
Q7. Write a JavaScript program to validate a password (at least 8 characters, containing at least one uppercase letter, one number, and one special character) using a regular expression.
Input: "Password123!"
Expected Output: true
Q8. Write a JavaScript program to validate a zip code (5 digits) using a regular expression.
Input: "12345"
Expected Output: true
Q9. Write a JavaScript program to validate a date in the format MM/DD/YYYY using a regular expression.
Input: "12/31/2023"
Expected Output: true
Q10. Write a JavaScript program to validate a time in the format HH:MM (24-hour format) using a regular expression.
Input: "14:30"
Expected Output: true
Q11. Write a JavaScript program to validate an IPv4 address using a regular expression.
Input: "192.168.1.1"
Expected Output: true
Q12. Write a JavaScript program to check if a string contains only spaces using a regular expression.
Input: " "
Expected Output: true
Q13. Write a JavaScript program to check if a string starts with "Hello" using a regular expression.
Input: "Hello World"
Expected Output: true
Q14. Write a JavaScript program to check if a string ends with "world" using a regular expression.
Input: "Hello world"
Expected Output: true
Q15. Write a JavaScript program to validate a string to contain only hexadecimal digits (0-9, a-f) using a regular expression.
Input: "a1f3b7"
Expected Output: true
Q16. Write a JavaScript program to validate a string to check if it matches a specific pattern like XXX-XXX where X is a number.
Input: "123-456"
Expected Output: true
Q17. Write a JavaScript program to validate a string that only contains valid hexadecimal color codes (e.g., #a1b2c3) using a regular expression.
Input: "#a1b2c3"
Expected Output: true
Q18. Write a JavaScript program to validate a string containing only uppercase letters using a regular expression.
Input: "HELLO"
Expected Output: true
Q19. Write a JavaScript program to validate a string containing only lowercase letters using a regular expression.
Input: "hello"
Expected Output: true
Q20. Write a JavaScript program to validate a string for a valid credit card number format using a regular expression.
Input: "1234-5678-9101-1121"
Expected Output: true
Q21. Write a JavaScript program to validate a string containing a valid 10-digit phone number (with or without dashes) using a regular expression.
Input: "123-456-7890"
Expected Output: true
Q22. Write a JavaScript program to validate a string to check if it contains a valid HTML tag.
Input: "<div>"
Expected Output: true
Q23. Write a JavaScript program to check if a string is a valid floating-point number using a regular expression.
Input: "12.34"
Expected Output: true
Q24. Write a JavaScript program to validate if a string is a valid 24-hour time format (HH:MM:SS) using a regular expression.
Input: "14:30:15"
Expected Output: true
Q25. Write a JavaScript program to validate a string to check if it is a valid number (positive or negative) using a regular expression.
Input: "-123.45"
Expected Output: true
Q26. Write a JavaScript program to validate if a string is a valid social security number (SSN) using a regular expression.
Input: "123-45-6789"
Expected Output: true
Q27. Write a JavaScript program to check if a string contains a valid base64 encoded string using a regular expression.
Input: "U29mdHdhcmUgRW5naW5lZXJpbmc="
Expected Output: true
Q28. Write a JavaScript program to validate a string for a valid hex triplet color code format like #ff00ff.
Input: "#ff00ff"
Expected Output: true
Q29. Write a JavaScript program to check if a string matches a valid JSON-like structure using a regular expression.
Input: "{"key": "value"}"
Expected Output: true
Q30. Write a JavaScript program to validate a username (should only contain letters and numbers, and be 5-12 characters long) using a regular expression.
Input: "user123"
Expected Output: true
Q31. Write a JavaScript program to validate an international phone number in the format +<country code> <area code> <number> using a regular expression.
Input: "+1 234 567890"
Expected Output: true
Q32. Write a JavaScript program to check if a string contains a valid date format YYYY-MM-DD.
Input: "2023-12-31"
Expected Output: true
Q33. Write a JavaScript program to check if a string matches a valid file extension (e.g., .jpg, .png, .txt).
Input: "image.jpg"
Expected Output: true
Q34. Write a JavaScript program to validate if a string matches a valid domain name (with a .com, .org, .net, etc.).
Input: "example.com"
Expected Output: true
Q35. Write a JavaScript program to validate an integer (positive or negative) using a regular expression.
Input: "1234"
Expected Output: true
Q36. Write a JavaScript program to validate a string to match a proper YYYY/MM/DD date format.
Input: "2023/12/31"
Expected Output: true
Q37. Write a JavaScript program to check if a string matches the MM/DD/YYYY date format using a regular expression.
Input: "12/31/2023"
Expected Output: true
Q38. Write a JavaScript program to validate a credit card number that starts with a 4 and has 16 digits using a regular expression.
Input: "4123-5678-9101-1121"
Expected Output: true
Q39. Write a JavaScript program to validate if a string contains only non-space characters.
Input: "validString123"
Expected Output: true
Q40. Write a JavaScript program to validate an address (should include a number, street name, and city).
Input: "1234 Main St, Springfield"
Expected Output: true
Q41. Write a JavaScript program to validate if a string only contains printable ASCII characters using a regular expression.
Input: "Hello World!"
Expected Output: true
Q42. Write a JavaScript program to check if a string contains a valid currency format (e.g., $123.45).
Input: "$123.45"
Expected Output: true
Q43. Write a JavaScript program to validate a string to check if it is a valid time in the 12-hour format (hh:mm AM/PM) using a regular expression.
Input: "12:45 PM"
Expected Output: true
Q44. Write a JavaScript program to validate a string for a valid 6-digit hexadecimal color code.
Input: "#a1b2c3"
Expected Output: true
Q45. Write a JavaScript program to check if a string is a valid IP address with a port number.
Input: "192.168.1.1:8080"
Expected Output: true
Q46. Write a JavaScript program to check if a string contains a valid hexadecimal color code starting with # and followed by six characters.
Input: "#ff5733"
Expected Output: true
Q47. Write a JavaScript program to validate if a string only contains printable characters excluding control characters using a regular expression.
Input: "Valid String"
Expected Output: true
Q48. Write a JavaScript program to validate if a string matches the format of a decimal number (with optional signs and decimals).
Input: "-123.45"
Expected Output: true
Q49. Write a JavaScript program to check if a string contains a valid username (starting with a letter, containing letters and numbers only, 6-12 characters).
Input: "user123"
Expected Output: true
Q50. Write a JavaScript program to check if a string contains only uppercase letters and is at least 6 characters long using a regular expression.
Input: "HELLO"
Expected Output: true