Advertisement

03.01.2008 at 07:54PM PST, ID: 23207026
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.7

Javascript / Client-Side Validation / RegEx Question...

Asked by joejenkinsjax in JavaScript, Regular Expressions

Tags: , ,

I have been working on an intranet inventory application that exclusively uses MSIE 6 and MSIE 7 as the browser interface.  

We have a form that is dynamically created using ASP and are currently using Javascript to limit which keys may be pressed when entering the inventory values into the text input fields.  We also validate to make sure (for whatever reason) that a value doesn't get past the onKeyPress event and put in the input box.  I'm having no problem only allowing numbers and the period character but there is one piece I haven't been successful in preventing.  Since a period is a valid keypress, it lets multiple periods go through.  Obviously, this chokes on submit.  

I need a way to make the code either strictly disallow input of multiple periods or onBlur validate the text input value and return focus back to the erroneous block, displaying an error.  

Acceptable value examples are: 10 | 1.50 | .50

I have found a regex that seems to work well in my tester if regex is the right solution:
^(-)?(\d+(\.\d*)?|\.\d+)$

Below, the onBlur code is "validateNumber()" and onkeypress code is "handlekeypress" :Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
function validateNumber() {
    // Get the source element.
     var el = event.srcElement;
     // Valid numbers
     var num = "0123456789.";
     event.returnValue = true;
if( el.value.length==0) { //if null is entered length will be 0
// alert("You MUST enter a quantity in the box");
     event.returnValue=false;
}
    /* Loop over contents. If any character is not a number,
        set the return value to false. */
     for (var intLoop = 0;
        intLoop < el.value.length; intLoop++)
        if (-1 == num.indexOf(el.value.charAt(intLoop)))
           event.returnValue=false;
          if (!event.returnValue) {      // Bad value
               el.className = "badValue"; // Change class.
			   alert("You MUST enter a value in the box \"0\" if NONE.");
			   el.focus();
			}
            else
               // Clear class to use default rendering.
               el.className="";
}
 
function handleKeyPress (field, event) {
if ((event.keyCode < 46) || (event.keyCode > 57) || (event.keyCode == 47)) 
	event.returnValue = false;
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
            var i;
            for (i = 0; i < field.form.elements.length; i++)
                if (field == field.form.elements[i])
                    break;
            i = (i + 3) % field.form.elements.length;
            field.form.elements[i].focus();
            return false;
        } 
        else
        return true;
    }
 
Loading Advertisement...
 
[+][-]03.01.2008 at 08:13PM PST, ID: 21024914

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 08:15PM PST, ID: 21024917

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 08:18PM PST, ID: 21024926

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, Regular Expressions
Tags: Javascript, MSIE 6+, Intranet
Sign Up Now!
Solution Provided By: b0lsc0tt
Participating Experts: 1
Solution Grade: A
 
 
[+][-]03.01.2008 at 08:47PM PST, ID: 21024991

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 08:57PM PST, ID: 21025021

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628