How to check the decimal & alphabets in a string using javascript?

Posted on 2007-09-28
Last Modified: 2008-01-09
How to check the decimal & alphabets in a string using javascript?
Ex: AB123FC
Numerals: 123
Alphabets: ABFC
Question by:SwamyN
    LVL 14

    Expert Comment

    To check decimals use this....

    use regular expression. try this:

    function isDecimal(field)

    var validTxt = /^[0-9.]+$/
    var msg = "Not a valid decimal.";
    if (! validTxt.test(field.value))
    return false;
    return true;

    or you can have this code instead:

       function IsNumeric(sText, name)
        var iNumber=(sText)*(-1)*(-1);
        var IsNumber=(!isNaN(iNumber));
        if (IsNumber == false) {
             alert("Please enter NUMBER only for "+name+"!");
        return IsNumber;  

    LVL 14

    Expert Comment

    by:bugs021997 bad don't use the comment i posted above...
    LVL 19

    Accepted Solution

    var str = 'AB123FC';
    var numbers = str.replace(/[^0-9]/g, '');
    var letters = str.replace(/[^A-Za-z]/g, '');

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now