[RESOLVED]validation for Only +ve Numbers

  function ValidateTextFieldsData()
    {        
     
        var obj = ($('.textActivityValidation'));
        var value = $.trim($('.textActivityValidation').val()); 
        if(value == "")
        {
            $("#Validate_Order").show();
            returnValue = false;
        }       
            
        else // here for  number validation
        {
            //    var key = e.keyCode;
            //    alert('key'+key);
            //    if((key < 48 || key > 57)) {
            //        alert(1 +'event.keyCode ='+event.keyCode );
            //    }

            //var isWhole_re       = /^s*d+s*$/;
            
            reg = /[^0-9.,]/g;
            obj.value =  obj.value.replace(reg,"");
    


        }
           

            

        return returnValue;
    }

I want to put required validation  which is working fine, but now i have to validate the only +ve number is allowed in the text box, if value is ok then return true else false & stops.

You can use the below updated code which validates empty value as well as positive number also

 function ValidateTextFieldsData()
    {        
        var obj = ($('.textActivityValidation'));
        var value = $.trim($('.textActivityValidation').val()); 
      
          
        if(value == "")
        {
            $("#Validate_Order").show();
            returnValue = false;
        }       
       else // here for  number validation
        {
            //Code to validate if it s positive number or not
           returnValue = value == "0" || ((value | 0) > 0 && value % 1 == 0);
        }
       
       return returnValue;
    }

Complete Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <script>
    function ValidateTextFieldsData()
    {        
        var obj = ($('.textActivityValidation'));
        var value = $.trim($('.textActivityValidation').val()); 
      
          
        if(value == "")
        {
            $("#Validate_Order").show();
            returnValue = false;
        }       
       else // here for  number validation
        {
            //Code to validate if it s positive number or not
           returnValue = value == "0" || ((value | 0) > 0 && value % 1 == 0);
        }
       
       return returnValue;
    }
  </script>
</head>
<body>
  <input type="text" value="" class="textActivityValidation">
<input type="button" value="total" name="B3" onclick="return ValidateTextFieldsData();">
</body>
</html>

You should be able to use the following string to validate all numeric strings (along with decimal points and commas) :

reg = /^[d.,]$/;

It’s important to note the ” characters that are required to escape some the period that you currently have (which matches all characters in Regular Expressions) and you can use the ‘d’ character to match any digits (0-9).

You can see a very basic working example below :

<script type='text/javascript'>
    $(function(){
       // When your button is clicked
       $('#validate').click(function(){
            // Get the trimmed contents of your example TextBox
            var value = $.trim($("#example").val());
            
            // Ensure it isn't empty
            if(value.length <= 0){
                // It's empty, enter a value
                alert("A value is required!");
                return false;
            }
         
            // Define your expression
            var re = /^[d.,]$/;
         
            // Check if it meets your requirement
            if(re.test(value)){
               // It is valid
               alert("Valid!");
               return true;
            }
            else{
               // Invalid
               alert("Invalid!");
               return false;
            }
       });
    })
</script>

Thum

A2H

You can use the below updated code which validates empty value as well as positive number also

 function ValidateTextFieldsData()
    {        
        var obj = ($('.textActivityValidation'));
        var value = $.trim($('.textActivityValidation').val()); 
      
          
        if(value == "")
        {
            $("#Validate_Order").show();
            returnValue = false;
        }       
       else // here for  number validation
        {
            //Code to validate if it s positive number or not
           returnValue = value == "0" || ((value | 0) > 0 && value % 1 == 0);
        }
       
       return returnValue;
    }

Complete Code:

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <script>
    function ValidateTextFieldsData()
    {        
        var obj = ($('.textActivityValidation'));
        var value = $.trim($('.textActivityValidation').val()); 
      
          
        if(value == "")
        {
            $("#Validate_Order").show();
            returnValue = false;
        }       
       else // here for  number validation
        {
            //Code to validate if it s positive number or not
           returnValue = value == "0" || ((value | 0) > 0 && value % 1 == 0);
        }
       
       return returnValue;
    }
  </script>
</head>
<body>
  <input type="text" value="" class="textActivityValidation">
<input type="button" value="total" name="B3" onclick="return ValidateTextFieldsData();">
</body>
</html>

 Thumbs up for modulus logic,

One consideration regarding A2H’s recommended solution is that it does not handle floating point numbers (for instance 1,000.52 would be negative) which your Regular Expression in your original question indicates might come up : 

If you need to check if a particular value in positive or negative, you could simply parse the value as an floating point number(after stripping out any commas or other formatting strings) using the parseFloat() function and then just check if it
is positive or negative :

function ValidateTextFieldsData(){        
        // Store a reference to your object
        var obj = $('.textActivityValidation');
        var value = $.trim(obj.val()); 
        
        // Parse your value as a float
        var x = parseFloat(value.replace(/,/g,''));
  
        // Check if it exists at all
        if(x == undefined && x.toString().length > 0){
            // The number is either empty or invalid
            alert("The number is either empty or invalid")
            $("#Validate_Order").show();
            returnValue = false;
        }       
         
        // Validate 
        var isNegative = x > 0;
      
        // Output a message
        alert(isNegative ? 'Positive!' : 'Negative');
      
        // Return if it was negative or not
        return isNegative;
}

You can see an example of this here.

Leave a Reply