[RESOLVED]Regular expression to validtate sum of numerics

I want to validate the user input,

it should accept,

  • 1+2
  • 1.2+56+3.5

it should not accept any alphabets, special characters other than . and +

and mainly it should not accept ++ and .. 

please help me with regular expression.

So basically, you want to have a string that the following :

  • Allows only numeric characters, ‘+’ and ‘.’
  • Doesn’t allow consecutive ‘+’ or ‘.’ characters (as these would likely be invalid)
  • Doesn’t begin or end with ‘+’ or ‘-’ (as these would likely be invalid)

Using those constraints, you would get the following expression :


You can see an explanation below :

^                  # Beginning of expression
(?!.*[+.]{2}.*)  # Don't allow repeating decimal points (.) or plus signs (+)
d                 # The first character must be a digit (can't start with decimal or plus)
[d.+]*          # Only allow digits, decimal points and plus signs in your string
d                 # The last character in the string must be a digit
$                  # End of expression

Here is an example of various tested input :

Hi Rion,

Thanks!!! I tried your expression in http://jsfiddle.net/DwKZh/763/ 

But it still allowing me to enter some invalid characters. 

Hi Mani,

Thanks for your post.

As for your problem, you could try the below sample by using javascript.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="../../Scripts/jquery-1.8.2.js"></script>
        $(function () {
            var bc = 0;    //the keycode, user entered before 
            var flag = 0; 
            $("#txt1").keydown(function (e) {
                var kc = e.keyCode;

                //when user input the first character, he can't input . or +
                if (bc == 0 && ((kc == 110) || (kc == 107))) {
                    return false;

                if ((kc >= 48 && kc <= 57) || (kc >= 96 && kc <= 105)) {
                    bc = kc;
                    return true;

                //. and +
                if ((kc == 110) || (kc == 107)) {

                    if ((bc == 110) || (bc == 107)) {
                        //user can't input ".." , ".+" , "+.", "++"
                        return false;
                    else {
                        //prevent inputing some data as "1.2.3"
                        if (flag == kc && flag==110) {
                            return false;
                        } else {
                            flag = kc;
                            bc = kc;
                            return true;

    <form id="form1" runat="server">
            <input id="txt1" type="text" />

Hope it will be helpful to you.

Best Regards,

Fei Han


But it still allowing me to enter some invalid characters. 

This is likely because you didn’t escape the ‘.’ character which is a reserved character in Regular Expressions to match "anything", make the following change from :

var transformedInput = inputValue.replace(/[^0-9+.]/g, ''); 

to :

var transformedInput = inputValue.replace(/[^d+.]/g, ''); 

You can see an example here.

Hi Rion,

Sorry for delay. Can you please put your regular expression


 on the same fiddle link and try to enter 1++2 (invalid input).

This is probably due to the fact that the expression that I provided above will validate if your specific input string matches the pattern that you needed (e.g it would validate if your entire string consisted of numbers, decimal points and plus symbols
without repeating decimals or pluses
). If you needed to use it that way, you would just need to define your expression and then simply check if your string matches or not :


In your case, it looks like you are replacing some values that might be invalid. You can resolve this by handling your repeating ‘+’ and ‘.’ characters explicitly by changing your existing code from :

var transformedInput = inputValue.replace(/[^d+.]/g, ''); 

to :

var transformedInput = inputValue.replace(/[^d+.]/g, '').replace(/+{2,}/g,'+').replace(/.{2,}/g,'.'); 

 This will replace any instances of repeating decimal values or plus characters to a single instance of that specific character.

Leave a Reply