Please provide pattern block links and any special characters as input

I have a promotion box where users can input text to send to administrators.

Objectives

  1. I need to prevent users from inputting links and special characters.
  2. Users should only be able to input letters a-z (case insensitive) and numbers 0-9.
  3. Input is optional.

Sample Code

Documentation

this.$prompt('Enter your message here.', 'Note', {
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    type: 'Thank You!',
    inputPattern: /[A-Za-z0-9]+/,
    inputErrorMessage: 'Invalid Message'
}).then(({ value }) => {
    // do something...
}).catch(() => {
    // do something...
});

Query

What should be the correct value for inputPattern for the desired functionality mentioned above?

Answer №1

It appears that you have overlooked the two rules requiring your test to match the entire string:

/^ .... $/

Additionally, to make the input optional, you should replace the + symbol (meaning 1 or more) with * (indicating 0 or more).

For instance:

var testcaseInputs = [
  "niceone", // yes
  "niceone1", // yes
  "NiceOne1", //yes 
  "nice-one-1", // no
  "", // yes
  " ", // no
];
var inputPattern = /^[A-Za-z0-9]*$/;

testcaseInputs.forEach(testcase => {
  console.log(testcase, new RegExp(inputPattern).test(testcase));
});

If you wish to allow strings with spaces, you can use this alternative regex 🙂 /^[A-Za-z0-9\s]*$/ (includes \s to accept whitespace characters)

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

Refresh the DIV element that houses dynamically generated <ul> HTML content after submitting new inputs

UPDATE: After delving into the code of nested include files in the child PHP file, I've identified conflicts with the parent PHP include files. This means that the child PHP file/div relies on the parent being refreshed, preventing me from refreshing ...

What varieties of ajax styles are there?

Although I am still relatively new to utilizing ajax, I have found a lot of success in my endeavors so far. The majority of my ajax calls tend to follow this format: function saveQueryProf(){ var currentDate = new Date(); var date=currentDate. ...

The local server for handling HTTP requests has ceased to operate

Recently, I set up the NPM package along with the http server within the "server" directory. Initially, everything was functioning smoothly; however, the server abruptly ceased operating. Upon attempting to launch the local http server, an error message a ...

Why am I unable to use a string as the src in next/image component?

After importing the Image module with the code import Image from "next/image";, I encountered an error that states: The type '{ src: string; }' cannot be assigned to type 'IntrinsicAttributes & ImageProps'. The type &apo ...

Providing parameters to a function. What could be the issue?

Here is a link to my code on jsFiddle I am attempting to pass an argument to a function, but it seems like the argument is not being received or executed. <a href="javascript:addRemove('7249');">Details</a> This code snippet uses J ...

submitting URL from dropdown menu without using the 'submit' button

My situation involves a dropdown list: @Html.DropDownList("InnerId", Model.GroupDropDownList, new { @class = "select_change" }) I am looking to achieve submitting the value when a user clicks on the selection without needing to select and then use a subm ...

Having trouble sending values via POST request in Node.js using Express

Currently, I am in the process of learning how to use Express (v4) with Node.js. My main goal right now is to create a basic REST API. This API specifically focuses on one endpoint: /orders. The main functionality I am trying to achieve is the ability to r ...

Troubleshooting: Issues with Mixins in Vue Test Utils and Jest

I am facing an issue with a local Vue app where I have added a method to all components using mixins. However, when I mount the app, the method does not seem to be mixed for child components. Below is my code snippet: import { createLocalVue, mount } from ...

Tips for ensuring that your modal functions properly with an image tag within a figure tag

I'm facing an issue with getting a modal to display on my image gallery. The problem arises when the images are enclosed within figure tags, necessary for my hover effect, causing the modal to malfunction. I suspect that the problem lies within the f ...

Activate Pop-up for a single instance on BigCommerce

After researching and adding my own code, I am still struggling to get this question answered correctly. Here are the key points I am trying to achieve: 1. Automatically open a popup when the homepage loads. 2. Ensure that the popup is centered on all brow ...

Ways to retain the page state even after refreshing or quitting the browser?

I am in the process of developing a quiz application that utilizes Laravel for the backend and Vue.js for rendering questions on the frontend. One issue I'm grappling with is how to maintain the quiz's state even if a candidate reloads the page o ...

Exploring Clara.io's json data for 3D geometry within the Three.js

I've encountered an issue with exporting models in Clara.io. According to their instructions, exporting a selection should create a file for JSONLoader and exporting the full scene should result in a file for ObjectLoader. However, none of the export ...

What is the best way to import API Endpoints from various directories in an Express Server?

I have been using a script to load my API endpoints like this: readdirSync('./routes/api').map((r) => app.use( `/api/v1/${r.split('.')[0]}`, require(`./routes/api/${r.split('.')[0]}`) ) ); This script reads eve ...

Unable to import JSX/TSX component from a separate React application into my primary React application

I find myself facing a challenge with integrating two React applications with different setups. Background: I was assigned the task of developing a design system using ReactJS that would be implemented in their primary application. Despite my limited kn ...

Struggling to implement a vertical scroll bar in HTML code?

<body ng-app="myApp" ng-controller="myCtrl"> <div ng-show = "dataFromRest" ng-repeat = "x in dataFromRest.posts" > <div class="tittle" style="width: 25%;"> <a href="" ng-click="showDi ...

Could there be an issue with the way I've implemented my setInterval function?

I am currently in the process of developing a stopwatch feature using React Native and implementing the setInterval function to increase a counter and update the state accordingly: Play Function (triggered upon pressing the play button) const [isRunning ...

Error: Issue with hook function call detected. Struggling to locate exact source in React JS

As I dive into React for the first time, I'm working on creating a sign-up form with multiple steps. Despite reading through the material-ui documentation and learning about ReactJS, I'm struggling to pinpoint where I've gone wrong. Here&ap ...

How can I combine these scripts that are not working simultaneously?

I have two scripts on my site that are based on the meta title, and I'm trying to make them work together. I thought changing the function names would be enough, but when I use both scripts, one doesn't work. Why is this happening? Also, should I ...

Using both Promise based architecture and events in Node.js can lead to unexpected behavior and should be avoided

Currently, I am developing a nodejs application that is expected to grow in size. Despite my efforts, I have not been able to find many resources on advanced Nodejs project architecture and structure. I am wondering if it would be considered bad practice ...

Tips for implementing personalized/modified CSS on Vuetify components?

Let's say I've included the v-text-field component from Vuetify in my Vue component as shown below: <v-text-field v-model="email" name="email" type="email" color="#90C143" label="Email"> Upon inspecting the element, it generates regular H ...