JavaScript Validation with regular expression - Exercises, Practice, Solution
JavaScript Validation with regular expression [21 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts.]1. Write a JavaScript program to test the first character of a string is uppercase or not. Go to the editor
Click me to see the solution
2. Write a JavaScript program to check a credit card number. Go to the editor
Click me to see the solution
3. Write a pattern that matches e-mail addresses. Go to the editor
The personal information part contains the following ASCII characters.
- Uppercase (A-Z) and lowercase (a-z) English letters.
- Digits (0-9).
- Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
- Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the other.
4. Write a JavaScript program to search a date within a string. Go to the editor
5. Write a JavaScript program that work as a trim function (string) using regular expression. Go to the editor
6. Write a JavaScript program to count number of words in string. Go to the editor
Note :
-
Remove white-space from start and end position.
- Convert 2 or more spaces to 1.
- Exclude newline with a start spacing.
7. Write a JavaScript function to check whether a given value is IP value or not. Go to the editor
8. Write a JavaScript function to count the number of vowels in a given string. Go to the editor
Test Data :
console.log(alphabetize_string('United States'));
Output :
"SUadeeinsttt"
9. Write a JavaScript function to check whether a given value is an valid url or not. Go to the editor
10. Write a JavaScript function to check whether a given value is alpha numeric or not. Go to the editor
11. Write a JavaScript function to check whether a given value is time string or not. Go to the editor
12. Write a JavaScript function to check whether a given value is US zip code or not. Go to the editor
13. Write a JavaScript function to check whether a given value is UK Post Code or not. Go to the editor
14. Write a JavaScript function to check whether a given value is Canada Post Code or not. Go to the editor
15. Write a JavaScript function to check whether a given value is a social security number or not. Go to the editor
16. Write a JavaScript function to check whether a given value is hexadecimal value or not. Go to the editor
17. Write a JavaScript function to check whether a given value is hexcolor value or not. Go to the editor
18. Write a JavaScript function to check whether a given value represents a domain or not. Go to the editor
19. Write a JavaScript function to check whether a given value is html or not.Go to the editor
20. Write a JavaScript function to check a given value contains alpha, dash and underscore. Go to the editor
21. Write a JavaScript function to print an integer with commas as thousands separators. Go to the editor
Test Data :
console.log(thousands_separators(1000));
"1,000"
console.log(thousands_separators(10000.23));
"10,000.23"
console.log(thousands_separators(100000));
"100,000"
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Live Demo:
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
JavaScript: Tips of the Day
How to insert an item into an array at a specific index (JavaScript)?
What you want is the splice function on the native array object.
arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert). In this example we will create an array and add an element to it into index 2:
var arr = []; arr[0] = "Jani"; arr[1] = "Hege"; arr[2] = "Stale"; arr[3] = "Kai Jim"; arr[4] = "Borge"; console.log(arr.join()); arr.splice(2, 0, "Lene"); console.log(arr.join());
Ref: https://bit.ly/2BXbp04
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework