13

I am fairly new. I am creating a webpage that ask a user for their ID. I want it to be a required field and only allow numbers. I appreciate if you lead me in the correct direction. this is what I have so far.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>

<html>
<head runat="server">
  <title>Search</title>
</head>
<body>
 <div>
  <table align="center">
    <tr>
        <td class="label">
            Enter ID:
        </td>
         <td>
            <input type="text" name="UserId" id="UserId" />
        </td>
     </tr>
   </table>
  </div>
 </body>
</html>
2
  • 1
    off the topic: Without JavaScript !!! any specific reasons? I'd love to see the solution though... Commented Sep 21, 2012 at 15:28
  • There is no reason. I looked online and it only give me solution with javascript. It seems to me that its much simpler if I just do it in html. Commented Sep 21, 2012 at 15:31

4 Answers 4

40

Though it's probably suggested to get some heavier validation via JS or on the server, HTML5 does support this via the pattern attribute.

<input type= "text" name= "name" pattern= "[0-9]"  title= "Title"/>
Sign up to request clarification or add additional context in comments.

6 Comments

Just tried this to no avail :( any chance you could take a peek here? stackoverflow.com/questions/17999067/…
@Davo This is an HTML5 attribute, so it won't work on older IE's without some polyfill. You can check for support here: developer.mozilla.org/en-US/docs/Web/HTML/Element/Input
@Davo No problem. To do that keyboard blocking, you can see this code here: stackoverflow.com/a/19966612/830111
Not to be overly pedantic but the OP used the word "numbers" (plural). Doesn't the pattern on this answer allow only a single digit? This will also not allow negative numbers or decimal places. You may want to look here for a more thorough answer stackoverflow.com/questions/16460773/…
If the field is required, update the pattern attribute to include regex's "one or more" quantifier ([0-9+]). If optional, you can use "zero or more" ([0-9*]). If you need to allow for decimals, leverage the step attribute: developer.mozilla.org/en-US/docs/Web/HTML/Element/input/…
|
24

Try this with the + after [0-9]:

input type="text" pattern="[0-9]+" title="number only"

2 Comments

What's the use of +?
@Niby Because without + accept only one number if you have some validation!
14

Of course, you can't fully rely on the client-side (javascript) validation, but that's not a reason to avoid it completely. With or without it, you have to do the server-side validation anyway (since the client can disable javascript). And that's just what you're left with, due to your non-javascript solution constraint.

So, after a submit, if the field value doesn't pass the server-side validation, the client should end up on the very same page, with additional error message specifying the requested value format. You also should provide the value format information beforehands, e.g. as a tool-tip hint (title attribute).

There's most certainly no passive client-side validation mechanism existing in HTML 4 / XHTML.

On the other hand, in HTML 5 you have two options:

  • input of type number:

    <input type="number" min="xxx" max="yyy" title="Format: 3 digits" />
    

    – only validates the range – if user enters a non-number, an empty value is submitted
    – the field visual is enhanced with increment / decrement controls (browser dependent)

  • the pattern attribute:

    <input type="text" pattern="[0-9]{3}" title="Format: 3 digits" />
    <input type="text" pattern="\d{3}" title="Format: 3 digits" />
    

    – this gives you a full contorl over the format (anything you can specify by regular expression)
    – no visual difference / enhancement

But here you still rely on browser capabilities, so do a server-side validation in either case.

2 Comments

Thank You so basically, I need java script? Will do thank you
Wait a minute. HTML5 seems to provide some improvements. Will report in a while.
0

You can also use

<input type="number" name="amount" pattern="^[0-9]+[0-9]*$"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.