Add class to span if it contains a decimal number

html input numbers only
input type=number decimal
input type=number'' min max
input type=number'' max value
input type=number'' max length
input type=number'' min max validation
input type=number no decimal
css apply to parent if child has class

I have a list of links as follows:

<span><span class="prefix">1.</span>xxx</span>
<span><span class="prefix">2.</span>xxx</span>
<span><span class="prefix">2.1.</span>xxx</span>
<span><span class="prefix">10.</span>xxx</span>

I want to add a class to the .prefix elements of that list which contains a decimal number. So far, I have reached the point where I count characters, and if > 2, class in added. But of course then the 10+ numbers are affected as well, and they shouldn't...

$(function() {
  $(".sub-menu span[class*='prefix']").each(function() {
    $(this).addClass($(this).text().length > 2 ? "sub" : "dom");
  });
});

I basically need to know how to treat the .text() element to discriminate between decimal and non decimal. The problem is I'm using a dot (".") as decimal comma, but also at the end of each numer -- so I can't even use something like ":contains:'.'" I guess what I need to do is to specify a discrete series of dots as discriminators between decimal and non decimal.

With match you should be able to do this, since you can use regular expressions.

Sadly regex isn't my strong suit. So i am not sure that it is correctly written, but the logic is there

$(function() {
  $(".sub-menu span[class*='prefix']").each(function() {
    var currentText = $(this).text();
    //$(this).addClass($(this).text().length > 2 ? "sub" : "dom");
    if( currentText.match(/\.\d+/g) ).length{
        //Do something - like adding the class
   }
   else{
        //Do something else - or skip
   }

  });
});

The regex here, should only be true if .is present, and is followed by a digit.

Meaning that 2.1 is true

2.1.3 is true

2. is false

2.1.5.is false

But maybe somebody can help with the regex

parsing decimals into a span, toFixed is a method that you would call on a number, not on a string. You need to parse the string to a number: var val = parseFloat($(this).val(),  Add(Decimal, Decimal) Adds two specified Decimal values. Ceiling(Decimal) Returns the smallest integral value that is greater than or equal to the specified decimal number. Compare(Decimal, Decimal) Compares two specified Decimal values. CompareTo(Decimal) Compares this instance to a specified Decimal object and returns a comparison of their relative values.

If you are sure your numbers will be valid numbers, (e.g: not 1.2.3, etc) you can check the module of this number by 1.

$(function() {
  $(".sub-menu span[class*='prefix']").each(function() {
    $(this).addClass(Number($(this).text()) % 1 ? "sub" : "dom");
  });
});

Any integer % 1 = 0 Any decimal % 1 > 0 (Unless it's something like 3.0 which could be represented as just 3)

Selectors, Although the intention of these rules is to add emphasis to text by changing its color, the effect The second selector will only match when the "href" attribute has the value EXAMPLE with an attribute "notation" that has a default value of "​decimal". Thus, for HTML, div.value and div[class~=value] have the same meaning. if the class/id contains a number greater than = or greater than 1 I'm unsure of exactly what you mean, you want to check for a class like something-2 ? var = myCondition = true; $('#over').toggleClass('readMe', myCondition); toggleClass might be what you're after.

You have to try following code :

    $(function() {
       $(".sub-menu span[class*='prefix']").each(function() {
         var num = $(this).text();
         if(num % 1 != 0)
           $(this).addClass("xyz")  
      });
   });

<input type="number">, If the value of the max attribute isn't a number, then the element has no maximum value. If you want to enter a value that requires decimals, you'll need to reflect this Then we add some CSS to narrow the width of the element with the id div { margin-bottom: 10px; } input:invalid+span:after { content: '✖'  This string class is in namespace std and can be accessed by including the string header file and using some technique to gain access to namespace std. (see vijayan's examples) There is a learning curve in terms of using either type string, but more and more people are recommended beginners in C++ learn how to use the C++ style strings right

Formatting Numbers | Using Numbers, The first, round(), rounds a value to a specified number of decimal places. This can be either a number or a variable that has a numeric value. After all the calculations but before the print statement, add the following (Script 4.3): <br> 50 After your $<span class=\"number\">$discount</span> discount,  Decimal must be in the range of -999.999.999.999.999,99 - 999.999.999.999.999,99. This problem occurs in the following products: Microsoft Dynamics NAV 2009 Service Pack 1 (SP1)

Input group · Bootstrap, Easily extend form controls by adding text, buttons, or button groups on either side of amount (with dot and two decimal places)"> </div> <div class="input-​group"> Screen readers will have trouble with your forms if you don't include a label  A Decimal Number (based on the number 10) contains a Decimal Point. Here is the number "forty-five and six-tenths" written as a decimal number: The decimal point goes between Ones and Tenths. 45.6 has 4 Tens, 5 Ones and 6 Tenths, like this: Now, let's discover how it all works It is all about Place Value !

Decimal.Parse Method (System), The span containing the characters representing the number to convert. style: NumberStyles. A bitwise combination of NumberStyles values that indicates the style  Converts the span representation of a number to its Decimal equivalent using the specified style and culture-specific format. A return value indicates whether the conversion succeeded or failed. TryParse(String, Decimal) Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion

Comments
  • Which of those values are you expecting to be valid/invalid?
  • you could use str.indexOf('.')
  • That doesn't answer my previous question.
  • Check the number of .s; if its > 1, add the "sub" class.
  • @HereticMonkey that's why I asked the OP to clarify what should/should not be valid, as from what I can see, all of the examples they show should be valid. They seem to have disappeared and left an annoying question which is far too open to interpretation though.
  • This might be a bit safer: /^[0-9]+.?$/g The regex checks if the start is any number with any count followed by an optional dot and nothing after the dot. E.g. Ten. would be false while it would be with Stenders Solution still true
  • @Sven Like I said - I suck at Regex :-D
  • No Problem, thats what the community is for - helping each other to get better :) :D