Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 599177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:28:10+00:00 2026-05-13T16:28:10+00:00

I am allowing a user to enter dimensions of a box through form fields.

  • 0

I am allowing a user to enter dimensions of a box through form fields. the permissible values are:

  1. positive integers
  2. must end with px or %

Sample valid numbers are 300px or 70%

In addition, I want to place some extra validation logic:

If entered value is in percentage, then the number must lie in the range 0 > x <=100

If the number entered is in px, I want to be able to check against hard coded min and max values.

My regex knowledge is pretty scanty, as I havent used it for many years.

I am think of writing a set of helper function like this:

// returns true if value ends in px or % [with no spaces], false otherwise
function is_scale_valid(value){
}

//returns 'px', '%' or undefined 
function get_scale_type(value){
}

// called after is_scale_valid, to ensure we are dealing with
// a +ve integer that falls in desired range. This function 
// strips away the 'scale' part of the value and only looks 
// at the number
function is_valid_number(value, scaletype, valid_min, valid_max){
}

can anyone help me fill out these helper functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:28:11+00:00Added an answer on May 13, 2026 at 4:28 pm

    The regular expression you want is:

    var r = new RegExp("^\\s*(\\d+(?:\\.\\d+)?)\\s*(px|%)?\\s*$");
    

    This expression means:

    • Allow leading and trailing white space (\s);
    • The number is one or more digits optionally followed by a period and one or more digits;
    • The units can be “px”, “%” or left out.

    You’ll need to manually check the inputted number from that.

    var match = r.exec("93px");
    var number = parseFloat(match[1]);
    var symbol = match[2];
    if (symbol == "px") {
      // validate number as a pixel
    } else if (symbol == "%") {
      // evaluate as percentage
    } else {
      // no symbol was entered
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form allowing a user to signup for a news letter which
I have an input form with two textareas allowing a user to type in
My app has a UISearchBar allowing user to enter search keywords. Each keystroke executes
I have a gridview, I am allowing user to enter HTML data while editing
I am using RavenDB and allowing a user to enter an item into the
I have to develop a web interface allowing the user to enter some inputs
I've got a numbers-only TextEdit box with MaxLength set to 2, allowing the user
How do I create a form in drupal with about 5 text fields, allowing
We made the mistake of allowing .csproj.user files to be checked in to TFS
What would be best practice for allowing the user to store data for my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.