20318

Regular expressions: JQuery check

Question:

I am working on a form text input that requires the user to enter an "a" followed by 6 proceeding numbers (0-9).

<blockquote> (a|)\d{6} </blockquote>

The JS does not allow the user to type in anything other than a string in this format.

However, I am having no luck. The example of my code is <a href="http://jsfiddle.net/rsheeler/3g5Ab/" rel="nofollow">located here</a>

Answer1:

It is a very bad idea to stop the user from typing anything they want. Here's why:

Let's say I miss the key I intended to press, and end up entering this sequence of keystrokes:

<blockquote>

<kbd>a</kbd><kbd>1</kbd><kbd>2</kbd><kbd>3</kbd><kbd>r</kbd><kbd>Backspace</kbd><kbd>4</kbd><kbd>5</kbd><kbd>6</kbd>

</blockquote>

I would expect that my slip would have been erased by my backspace, and the rest of the input went through. Imagine how surprised I'd be to see "a12456" in there instead, because your code had already stopped me from typing <kbd>r</kbd>, which means that my <kbd>Backspace</kbd> erased the 3!

Instead, try this:

<input type="text" name="code" <b><i>pattern="a[0-9]{6}"</i></b> required title="Please enter an &quot;a&quot; followed by six digits." />

<a href="http://jsfiddle.net/mS5Z8/1/" rel="nofollow">Demo on JSFiddle</a>

This feature of HTML5 will prevent the form from being submitted if the user does not enter data in the correct format. This, combined with server-side confirmation, will work beautifully. Plus, it even works if the user has JavaScript disabled!

That said, for such a specific format, it may be more helpful to do this:

<pre class="lang-html prettyprint-override">a<input type="text" name="code" pattern="[0-9]{6}" required title="Please enter six digits" />

<a href="http://jsfiddle.net/mS5Z8/2/" rel="nofollow">Demo on JSFiddle</a>

In this way, the "a" is already there and doesn't have to be typed, leaving the user to only need to type the part that changes. On the server, just stick the "a" back on to it.

Recommend

  • Are there ways to get android app view hierarchy from another android app?
  • Installing MEAN Stack: npm -v module.js: 338 throw err; Error: Cannot find module './cache/cach
  • MSAccess and ODBC. Filter dialog doesn't show
  • automate ssh-keygen for github in powershell
  • Replace end characters of current URL with bookmarklet
  • Get last insert id of Postgresql
  • How to emulate integrated numeric keypad cursor keys in linux
  • Web browser lock-down: How to?
  • Is it possible to generate a unique numeric value for each row in an iSeries table without looping?
  • How to make a npm command, that executes two commands in the console (one after another)?
  • In VIm, how to remove all lines that are duplicate somewhere?
  • IE readonly textarea problem
  • fluidly dealing with multiple keypresses in ActionScript3
  • C++ - Is destructor called when a vector holds objects?
  • vi mode to emacs mode while on R
  • Visual studio 2015 keystroke with mouse button
  • HALF_PTR Windows data type
  • Is it safe to accept URL parameters for populating the `url_for` method?
  • How to enable mapping the private property of the entity
  • Why can't UI components be accessed from a backgroundworker?
  • how to change api_token column in token guard
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • sweetalert2 inputoptions from file in select example
  • How can I tell a form not to dispose a particular control when it closes?
  • jQuery: How to AJAXify WordPress Search?
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • SonarQube: Cannot deactivate rule with missing quality profile
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Trying to get the char code of ENTER key
  • PHP CURL timing out but CLI CURL works
  • Do I need to reset a Perl hash index?
  • Why querying a date BC is changed to AD in Java?
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • Trying to switch camera back to front but getting exception
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • Why joiner is not used after Sequence generator or Update statergy
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS