2

I have a form like the following:

<form action="/html/tags/html_form_tag_action.cfm" method="get">
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name_applicant" value="" maxlength="100" />
</td>
</tr>
<tr>
<tr><td>Date Of Birth:</td></tr>
<tr>
<td>Date:</td><td>
<select>
  <option value ="1">1</option>
  <option value ="2">2</option>
  <option value ="3">3</option>
  <option value ="4">4</option>
  <option value ="5">5</option>
  <option value ="6">6</option>
  <option value ="7">7</option>
  <option value ="8">8</option>
  <option value ="9">9</option>
  <option value ="10">10</option>
  <option value ="11">11</option>
  <option value ="12">12</option>
  <option value ="13">13</option>
  <option value ="14">14</option>
  <option value ="15">15</option>
  <option value ="16">16</option>
  <option value ="17">17</option>
  <option value ="18">18</option>
  <option value ="19">19</option>
  <option value ="20">20</option>
  <option value ="21">21</option>
  <option value ="22">22</option>
  <option value ="23">23</option>
  <option value ="24">24</option>
  <option value ="25">25</option>
  <option value ="26">26</option>
  <option value ="27">27</option>
  <option value ="28">28</option>
  <option value ="29">29</option>
  <option value ="30">30</option>
  <option value ="31">31</option>
</select>
</td>
<td>Month:</td>
<td>
<select>
  <option value ="Jan">Jan</option>
  <option value ="Feb">Feb</option>
  <option value ="Mar">Mar</option>
  <option value ="Apr">Apr</option>
  <option value ="May">May</option>
  <option value ="Jun">Jun</option>
  <option value ="Jul">Jul</option>
  <option value ="Aug">Aug</option>
  <option value ="Sep">Sep</option>
  <option value ="Oct">Oct</option>
  <option value ="Nov">Nov</option>
  <option value ="Dec">Dec</option>
</select>
</td>
<td>Year:</td>
<td>
<input type="text" name="first_name" value="" maxlength="4" />
</td>
</tr>
<tr>
<td>EmailID:</td>
<td>
<input type="text" name="first_name" value="" maxlength="25" />
</td><td>@gmail.com</td>
</tr>
<tr><td> </td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>

In this form I want to ensure that, in the text box, only text is accepted as input; and only numbers are accepted in the number box. How do I do it without using JavaScript ?

2
  • What do you mean by "numbers in the number box"? They are drop-downs, aren't they? Commented Jan 25, 2011 at 19:42
  • yes drop downs . sorry was in a hurry . Commented Jan 25, 2011 at 19:44

5 Answers 5

7

Javascript is the only way to validate the data on the client side before it is submitted. Since Javascript can be disabled, your server-side code should always validate any submitted data before using it.

Sign up to request clarification or add additional context in comments.

4 Comments

Client side should never be trusted, not even if your client were a binary executable app. Everything you need to believe to be true, needs to be validated on the server side, always.
Not only can Javascript be disabled, but people can always forge HTTP requests directly. Therefore HTML5 validation will be of no help security-wise. You absolutely have to check anything the server receives in input.
@mdrg I second that, there should be an automated comment for that :)
HTML5 has only an input type="number". There's nothing which prevents entering numbers in a field for alphabets only.
5

As everybody said, never trust client-side verification and HTML 5 can handle your problem, here is a example:

<style>
    body {
        font-family: arial;
        font-size: 15px;
    }

    br {
        clear: left;
    }

    label {
        float: left;
        width: 120px;
    }

    label, span {
        font-weight: bold;
        margin-bottom: 20px;
    }

    span {
        color: red;
        display: inline-block;
    }
</style>

<form action = "/html/tags/html_form_tag_action.cfm" method = "get">
    <!-- Regular expression took care of the "maxlength" attribute -->

    <label>Name: </label>
    <input autofocus = "autofocus" name = "name_applicant" pattern = "^\D{0,100}$" required = "required" type = "text" /> * <br />

    <label>Date of birth: </label>
    <input name = "name_applicant" required = "required" type = "date" /> <br />

    <label>Email Id: </label>
    <input name = "first_name" pattern = "^\D{0,15}\@gmail\.com$" placeholder = "[email protected]" required = "required" type = "email" /> *<br />

    <span>* Do not use numbers</span> <br />

    <input type = "submit" value = "Submit" />
</form>

Preview (Most people don't have Opera or Safari)

1 2 3 4e

1 Comment

Looks like this HTML5 stuff does not work in IE8. Although, they work well on Firefox and other browsers, I won't use it now as IE is still being heavily used by some people so the validation won't work.
2

Most likely, you cannot do this with just HTML. The exception is using HTML5, which you may or may not be able to use.

HTML5 has a pattern attribute that you can supply a regular expression to constrain what can be typed in. However, not all browser support HTML5 elements and attributes and it may not be 100% for you. More Info: http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#attr-input-pattern

You could use JavaScript to prevent it from happening and it isn't foolproof either as JavaScript can be turned off in the browser.

Your best bet would be to utilize a server-side scripting language (i.e. PHP, ASP) to verify the data submitted validates to what you expect and if not, prevent it from being submitted and throw an error. This is common practice and is sometimes used in combination with JavaScript constraints.

Comments

1

you can try valijate jquery plugin. of course it is a jquery plugin. but, it uses 'data-XXXX' attributes to do the validations. you can do most of the validation without writing javascript codes.

here is an example for achieving above requirement

<form class="valijate">
<input type="text" data-valijate="at_text_change" data-vjerr="text must not contain any letter or character" data-vjmustn="?l, ?c">
</form>

class valijate and data-vjmustn="?l, ?c" do the magic. it will be checked at every keystroke (data-valijate="at_text_change")

if the user has entered invalid data, user will not able to submit the form.

Comments

0

See this code example:

<form>
    <input type="text"
           name="Number(s) Only"
           pattern="[;0-9]+"
           title ="Must be format: 12345;67891; etc"
           placeholder="12345; 67891; etc."
           onfocusout="this.checkValidity() ? '' : alert('Must be format: 12345;67891; etc')"
           required>
</form>`

This will validate the box when you click off of it.

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.