

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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#?
- Querying only the field name and display only the id in MongoDB?
- JavaScript Regex to remove leading zeroes from numbers?
- How to find capital letters with Regex in MySQL?
- C# Program to display name of Current Thread
- Capitalize only the first letter after colon with regex and JavaScript?
- Python program to find top three mostly occurred letters from company name
- Reverse Only Letters in Python
- Regular expression to match numbers only in JavaScript?
- How to display only the current year in JavaScript?
- C# Program to display the name of the directory
- Python Program to check if String contains only Defined Characters using Regex
- Go through an array and sum only numbers JavaScript
Advertisements