regex 1-9999 for form validation

10,261

Solution 1

Try the below regex,

^(?:[1-9][0-9]{3}|[1-9][0-9]{2}|[1-9][0-9]|[1-9])$

DEMO

Solution 2

This doesn't exclude zero, but /^\d{1,4}$/ should do the trick.

Solution 3

Try using this

^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$
Share:
10,261
ajmajmajma
Author by

ajmajmajma

Designer and developer in the Boston area. I am passionate about : Web app design and development, Graphic Design, Responsive Web Design, New Media Design, Web Development and Integration, Project Coordination and Print Advertising Collaterals.

 Specialties: Adobe Suite, Responsive Web Development and Design, CMS Installation & Customization, UI/UX design and development, Photo Augmentation & Manipulation, SEO, With development in : Javascript, Angular/Node, React, Ember, D3, web sockets(socket.io), AJAX, jQuery, HTML5, CSS3, SASS/LESS, Yeoman, lodash/underscore, RubyGems, Play, Foundation, Bootstrap, Skeleton, handlebars, PHP, canvas, svg, velocity.js, three.js, MySQUL, MongoDB, Git, SVN. SOreadytohelp

Updated on June 04, 2022

Comments

  • ajmajmajma
    ajmajmajma almost 2 years

    I am trying to write some form validation, I need one of the inputs to be 1-9999. I know nothing about regular expressions ( never used them before) and here is my first attempt

    /^([1-9][1-9]|[1-9]|[1-9]\d|9999)$/
    

    Does not seem to want to work, can anyone help me? Thanks!

  • TaeKwonJoe
    TaeKwonJoe over 2 years
    This pattern works perfectly for my use case in Angular: <input type="number" class="form-control form-control-lg" name="play-time" [(ngModel)]="playTime" min="1" max="9999" minLength="1" maxlength="4" pattern="^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][‌​0-9])?$" digitOnly required />