Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Validate textbox only for integer and float numbers on a button click.

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 124
    Comment on it

    We can validate a textbox on a button click by calling a function and using jquery.

    I am using regex for integer and decimal numbers and match method for the comparison.

    Here is the code:

    <html>
    <header ng-controller="menuController">
        <h1 class="ui-title">Send tip</h1>
    </header>
    <section class="midArea">
        <div class="form-horizontal ui-content">
            <form class="form-horizontal" name="TipForm" class="form-signin" novalidate>
            <div class="form-group" style="padding-bottom:0px; margin-bottom:0px;">
                <div class="form-horizontal ui-content footer1">
                    <div class="row">
                    <div class="reply-text1 col-xs-6">
                       <input autocomplete="off" class="form-control" placeholder="custom tip amount" name="send-tip" type="text" value="" ng-model="chatInput" id="input-chat" style="font-size: 12px;font-weight: 500;" required>
                           <div style="margin-top: 6px;width: 270px;">
                            <span id="email-status" class="errorStatus" style="margin-left: 0px;font-size: 13px;"></span>
                           </div>
                       </div>
                    <div class="col-xs-6 send-button1 margin0">
                        <button class="btn btn-default fullwidth fullwidth&#95;blue" id="send-message" style="padding-top: 6px;" ng-click="Sendtipdata(TipForm,chatInput)">Send tip</button>
                    </div>
                    <div class="clr"></div>
                </div>
    </form>
    </section>
    </html>
    

    Now here is the code you have to include in your js:

    $scope.Sendtipdata = function(TipForm,chatInput){  
    var intRegex = /^\d+$/;
    var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
    
              if( $('#input-chat').val() != '' && ($( '#input&#95;chat' ).val().match( intRegex ) || $( '#input-chat' ).val().match( floatRegex ) ))
              {
                if($('#input-chat').val() >= 1){
                $( '#email-status' ).html('');
              }
              else{
                $( '#email-status' ).html('Please enter tip amount greater than or equal to 1.');
              }
            }
              else{
                $( '#email-status' ).html('Please enter a valid tip amount.');
              }
        };
    

    In the above code I have added some more validations like not null and greater than zero. You can modify according to your need. I hope this code snippet will help you to perform validation

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: