
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript regex program to display name to be only numbers, letters and underscore.
Following is the code to to display name to be only numbers, letters and underscore using regex in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>, letters and underscore only regex match</h1> <div style="color: green;" class="result"></div> <input type="text" class="txt" /&lgt; <button class="Btn"&g;CHECK</button> <h3> Click the above button to check if the text contains only numbers, letters and underscore or not </h3> <script> let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { var str = document.querySelector(".txt").value; var regex = /^\w+$/; var match = str.match(regex); if (match) resEle.innerHTML = "The text entered is valid"; else resEle.innerHTML = "The text enterd is not valid"; }); </script> </body> </html>
Output
The above code will produce the following output −
On entering text having space between them and clicking on ‘CHECK’ −
On entering valid text and clicking on ‘CHECK’ −
- Related Articles
- REGEX in MySQL to display only numbers separated by hyphen.
- MongoDB regex to display records whose first five letters are uppercase?
- How to write Regex for numbers only in C#?
- JavaScript Regex to remove leading zeroes from numbers?
- How to find capital letters with Regex in MySQL?
- Querying only the field name and display only the id in MongoDB?
- Java Program to Display Numbers and Sum of First N Natural Numbers
- How to display only the current year in JavaScript?
- Capitalize only the first letter after colon with regex and JavaScript?
- Python Program to check if String contains only Defined Characters using Regex
- Regular expression to match numbers only in JavaScript?
- C# Program to display name of Current Thread
- Python program to find top three mostly occurred letters from company name
- Regex in Python to put spaces between words starting with capital letters
- JavaScript - Accept only numbers between 0 to 255 range?

Advertisements