Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,

Below is the regular expression I used in Javascript for decimal numbers (Ex: 10.01, 121.122,..). But it allows string like "a" , "b" etc. If I give "aa" then the regulare expression works as expected

var regex=new RegExp("^[+-]?[0-9]*\.?[0-9]*$");

Any Idea where I made a mistake.

Thanks for helping me to sort this issue.

Regards,
Rajara.
Posted

C#
$('#YourField').change(function () {
        var v = parseFloat(this.value);
        if (isNaN(v)) {
            this.value = '';            
        } else {
            this.value = v.toFixed(2);           
        }
    });
 
Share this answer
 
Comments
Member 10768757 8-May-14 6:56am    
Hi,

Thanks for your time. But if I enter "1asd" it gives 1 and eliminated the remaining.
In mycase it should throw error.
Vi(ky 8-May-14 7:05am    
before using parseFloat, you can use the below pattern to ensure the input contains only numbers if not then throw error<br>
var pattern = /^[0-9-+]+$/; <br>
if (pattern.test(textfield)) == false){<br>
//do whatever you want<br>
}
try Regex:
JavaScript
var intRegex = /^(?:[1-9]\d*(?:\.\d\d?)?|0\.[1-9]\d?|0\.0[1-9])$/;
 
Share this answer
 
v2
Comments
Member 10768757 8-May-14 7:00am    
Sorry mate, it doesnt work.
Sanket Saxena 8-May-14 7:04am    
check updated solution dear
Member 10768757 8-May-14 7:39am    
Thank you !! It worked ..
Sanket Saxena 8-May-14 7:41am    
Great...Welcome...hey its downvoted strange might some novice did. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900