Pattern for regex to match the following example: test test123

Regular expression pattern for the test example "test test123". The first string should consist of only alphabets (A-Za-z), while the second string should be a combination of alphabets and numbers (A-Za-z0-9).

Examples: 1. hello world123 (true) 2. 123 hello123 (false)

Answer №1

You can utilize the regex ^[a-zA-Z]+[ ][a-zA-Z0-9]+$

function ValidateInput() {
    var string = document.getElementById("txtInput").value;
    var pattern = new RegExp("^[a-zA-Z]+[ ][a-zA-Z0-9]+$");
    var result = pattern.test(string);
    document.getElementById("output").innerHTML = result;
}
<input type="text" id="txtInput" />
<button onclick="ValidateInput()">Check</button>
<p id="output"></p>

Answer №2

I implemented the following code and it is functioning perfectly:

var pattern = new RegExp("^[a-zA-Z]+[ a-zA-Z0-9]+$"); var result = pattern.test(str);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The canvas is responsive to keyboard commands, however, the image remains stationary and unaffected

As I delved into the basics, incorporating canvas, CSS, and JavaScript, a question arose: should the image be housed in the HTML or the JavaScript code? Following this, I added a background color to the canvas and defined properties of the canvas in JavaSc ...

Global Redirect Pro is a cutting-edge redirection tool that

Check out the code below which successfully edits a link to redirect users to a specific website based on their location. I'm interested in enhancing this code in two ways: $(window).load(function () { $.getJSON('http://api.wipmania.com/json ...

How to implement pagination with subdocuments in Node.js using Mongoose

My attempt to paginate a subcomment named "Items" within a collection using mongoose-paginate-v2 has not been successful. Below is my model: const mongoosePaginate = require('mongoose-paginate-v2'); const PettyCashItemsSchema = ...

Store image selection in state

I am currently working on building an imagePicker in ReactNative, but I am running into an issue when trying to select the image. The error message I am receiving is: TypeError: this.setState is not a function. (In 'this.setState ({ avatar: data}) &a ...

Transferring data from local storage to a PHP server using AJAX

My attempt to transfer data from local storage to PHP using AJAX resulted in an error mentioning 'undefined index data'. chart.php <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="t ...

Step-by-step guide on setting an array value to a different input type with jQuery

Just starting out with jQuery and could use some help. How can I use jQuery to assign an array input value to another input field? <tr> <td><label>Date:</label><input type="date" name="PostingDate[]" id="date" style="text-al ...

Click on each item within the v-for loop to gather relevant information, and subsequently iterate through the collected data

Within a v-for loop, I have implemented a button that, when clicked, retrieves specific data. The objective is to display this data below or in place of the clicked button. <div v-for="(item, index) in items" :key="index"> <button @click="fetch ...

Dividing an array into categories with typescript/javascript

Here is the initial structure I have: products = [ { 'id': 1 'name: 'test' }, { 'id': 2 'name: 'test' }, { 'id' ...

Error encountered when generating bower.json due to absence of version number

After generating the bower.json file, I noticed that the version number was not included in the information collected. The current content is as follows: { "Name": "conFusion", "Authors": [ "Aurora" ], ... } Although the version n ...

jQuery does not have the capability to access the href attribute through DOM manipulation

I've been trying to extract the href attribute from a link in my code and create a new link using that attribute. However, I'm facing an issue where the created link doesn't seem to work properly - it keeps showing a 404 error message like t ...

The Jquery AJAX request was successful, but no data was returned

I am experiencing some issues with displaying the PHP response in my HTML. It seems like the if statements are not functioning properly. The PHP echoes are not being recognized by the success function. jQuery <script> $(document).ready(function(){ ...

Place items into the text box on an HTML webpage

Is there a way to add more than one element into a textbox using jQuery, and then insert them into a database? Currently, the script only adds one element at a time when I attempt to add another one that was previously removed. Any suggestions on how to ac ...

AngularJS Javascript calendar for selecting dates

I have developed a calendar that displays the current month and the next 12 months, along with the current date to the last date of the current month. For example, if today is October 20th, it will display dates from October 20th to October 31st. However, ...

Analyzing refined information using D3

After loading data, I implemented a filter feature using checkboxes. Every time the checkbox is updated, the date field in the data needs to be parsed. The script below achieves this successfully on the first click but encounters an error on subsequent c ...

During the installation of a package, npm encountered a require stack error with the code MODULE_NOT_FOUND

Whenever I attempt to install something using the npm install command, it throws an error saying "require stack" and "code MODULE_NOT_FOUND" C:\Users\dell>npm audit fix node:internal/modules/cjs/loader:1075 const err = new Error(message); ...

Angular DataTable jQuery

I am a huge fan of jQuery DataTable, but I have come to realize that when using AngularJS, it does not function properly. Instead of displaying the data with pagination, it shows "No data available in table Showing 0 to 0 of 0 entries." I have checked ou ...

Invoke a function from within an event handler

Is there a way to trigger a function once an event has been completed? For example - $('.class').slideUp('fast', function() { // execute the function }); ...

How to render in Express.js without overwriting HTML elements (such as <p> tags)

I am currently working on a project that resembles a blog and I have a requirement to display text without altering the HTML tags. ./app.js //app.js ... var text = 'Hello <b>World</b>' app.get('/', (req, res)=>{ res ...

How can I stop json_encode() from including the entire page in the JSON response when submitting a form to the same PHP script?

I only have a single index.php file in my project. I am aware that it's recommended to separate the logic from the view and use different files for PHP, JS, and HTML. This is just a test: <?php if($_SERVER["REQUEST_METHOD"] == "P ...

Error 500 occurs during an ajax call to the server, however, the call works fine when running on localhost

I encountered an issue with my JavaScript code when making an AJAX call to a specific server. The strange thing is that it works perfectly fine when I use localhost, but as soon as I try calling the service from the uploaded server, I get an ERROR 500. T ...