skip to Main Content

My pattern currently allows letters and does not allow spaces and numbers, intentionally. However, I would like to allow characters such as !@#$%^&*()_+=-[]|}{;’":,./?>< for my pattern and input type. How would I doe this?

I tried searching all over the internet and found lots of solutions but none of them worked for me.

Here is my code:

<html>
    <body>
        <form method="get">
            <input type="number" min="0" max="150" name="Age"/>
            <input type="text" name="Name" pattern="[A-Za-z]+"/>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

3

Answers


  1. [^s] matches any character that is NOT a whitespace character.

    <html>
        <body>
            <form method="get">
                <input type="number" min="0" max="150" name="Age"/>
                <input type="text" pattern="^[^s]+$">
                <input type="submit" value="Submit"/>
            </form>
        </body>
    </html>
    
    Login or Signup to reply.
  2. You can do it, but have in mind that source code in html is manipulable, so consider validate it in server side

    <html>
        <body>
            <form method="get">
                <input type="number" min="0" max="150" name="Age"/>
                <input type="text" name="Name" pattern="[A-Za-z!@#$%^&*()_+=-[]{}|&quot;;':,./?><]+"/>
                <input type="submit" value="Submit"/>
            </form>
        </body>
    </html>
    

    Service Side

    const express = require('express');
    const app = express();
    const bodyParser = require('body-parser');
    
    app.use(bodyParser.urlencoded({ extended: false }));
    
    app.post('/submit-form', (req, res) => {
        const name = req.body.Name;
        // Validate name using a regular expression
        const validName = /^[A-Za-z!@#$%^&*()_+=-[]{}|';:,./?><]+$/.test(name);
    
        if (!validName) {
            res.status(400).send('Invalid input for Name.');
            return;
        }
    
        // Process the valid input further
        // Example: save to database, perform business logic, etc.
    
        res.send('Form submitted successfully.');
    });
    
    app.listen(3000, () => console.log('Server is running on port 3000'));
    
    Login or Signup to reply.
  3. Try:

    [^ds]+
    

    Explanation:

    Any amount of characters []+ that is not ^ a number d or whitespace s.

    Examples:

    RegEx101

    <form method="POST" action="https://httpbin.org/post" target="response">
      <fieldset>
        <legend>Numbers and spaces are <b>not</b> permitted</legend>
        <input name="No_Number_or_Spaces" pattern="[^ds]+" style="display: inline-block; width: 87%" required value="!@#$%^&*()_+=-[]|}{;'&quot;:,./?><Aa">
        <button>Send</button>
      </fieldset>
      <fieldset>
        <legend>Response from live test server</legend>
        <iframe name="response" width="100%"></iframe>
      </fieldset>
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search