A Validation Style

I want my user input validation like this:

For digits fields, if a user enter a charcter, the box still stays empty. The box show values only digits are entered.

For decimal fields, the box only accepts decimal format otherwise the box stays empty.

How can I achive the purpose? Thanks.
LVL 1
minglelinchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guveraCommented:
Hi,

 
mNumValidator.ValidationExpression =  "^(\d|,)*\.?\d*$"

using this code in codebehind.

If you want client side, then using javascript. For this one plz look below link.
http://wiki.asp.net/page.aspx/1325/decimal-value-validation-and-round-off-by-javascript/

Thanks
Guvera

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anurag AgarwalCommented:
hi
use javascrpit
FOR NUMBERS only :
<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML

And smilarly for decimal filed!! Try this .
Anurag AgarwalCommented:
hi
for decimals
<script language="JavaScript" type="text/javascript">
function zxcFloat(obj){
obj.value=obj.value.replace(/[^-?\d\.]/g,'');
obj.value=parseFloat(obj.value);
}
and call this function  
<INPUT id="txtChar" onblur="zxcFloat(this)" type="text" name="txtChar">

Use client side scrpting as when user press key to enter it checks ,if invalid it remains empty . And also in decimal if user enter decimal two times ,second decimal deleted as user move the cursor from textbox. Try. And for message you n give alert or document.getelementid() etc.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

minglelinchAuthor Commented:
Thank you all. All answers are great.

isNumberKey is working fine for digit entry.
For decimal entery, isDecimalKey is working fine (the entry only accepts digits and dot), but when furhter checking the format at onblur or lostfocus event, the function seems like not firing at all.
I used -
 function isDecimalKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && ((charCode < 48 && charCode != 46) || charCode > 57))
            return false;
         
         return true;
      }
     
function isDecimalFormatMatch()
{
    var txtWDLost= document.getelementid("txtWDLost").value;
      if (txtWDLost.value.tostring().match(/^\d+\.\d{2}$/)) {
            return true; //alert("match");
      }
      else
      {
          alert("Invalid format.");
            //return false;
      }
}

<asp:TextBox ID="txtWDLost" runat="server" Width="190px" onkeypress="return isDecimalKey(event)" type="text" name="txtWDLost" onblur="return isDecimalFormatMatch()">

Anurag AgarwalCommented:
Anurag AgarwalCommented:
hey minglelinch:
I have tried this javascript ... onKeyUp event.
check this also.It even obstructs user to write second dot .


<html>
<head>
<script language="JavaScript">
  function Decimal( evt ) {
    var msg=evt.value; 
    var w;
    var str=msg.indexOf( "." );
    var ind;
      for ( w=0; w<msg.length; w++ )  {
      ind=msg.substring(w,w+1);
      if ( ind<"0" || ind>"9" )  {
    
           if ( str>0 )
             if ( w==str )  continue;    
       
           msg=msg.substring(0,w);
           evt.value=msg; 
           break;           
      }
    }
  }
</script>
<title>Decimal check</title>
</head>
<body>
<form name="form1" method="post" action="gonder.html">
  <input name="Txt01" type="text" value="" size="30" onKeyUp="Decimal(this)"><br>
</form>
</body>
</html>

Open in new window

minglelinchAuthor Commented:
Hey anurag_onnet:

I tested you code above. It's short and it's working fine except it accepts multple digits after dot.

I used the last version of MaskMoney function in the link you gave me. It's a nice one too.

Thank you. Happy New Year!
Anurag AgarwalCommented:
Hi minglelinch
Yes I have rechecked my code and made the changes as asked by you. Now only two digits after decimal as per function  used in that link.
Happy new Year to you too !!
<html>
<head>
<script language="JavaScript">

  function Decimal( evt ) {

    var msg=evt.value; 
    var w;
    var str=msg.indexOf( "." );
    var ind;
  
    for ( w=0; w<msg.length; w++ )  {
      ind=msg.substring(w,w+1);
      if ( ind<"0" || ind>"9" )  {
               if ( str>0 )
             if ( w==str )  continue;    
                  msg=msg.substring(0,w);
           evt.value=msg; 
           break;           
      }
    }
    }
    function chk(evt)
    {
var dec=evt.value.split('.');
if (dec[1].length>2)
return( alert('only two digit after decimal'));
}
</script>
<title>Decimal check</title>
</head>
<body>
<form name="form1" method="post" >
  <input name="Txt01" type="text" value=""
    size="30" onKeyUp="Decimal(this)"  onblur="chk(this)"><br>
</form>
</body>
</html>

Open in new window

minglelinchAuthor Commented:
Thank you, anurag_onnet. I tested it and it's working as you described. I will use it in the future.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.