• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

A specific length of digit entry javascript validation

I need a limited length of digits validation using javascript function.
Let's say a format with only 8 digits for a textbox. What would be a simple way to do it?
I did some validations but never did for a limited length.
Thanks.
0
minglelinch
Asked:
minglelinch
  • 5
  • 4
  • 2
  • +2
2 Solutions
 
Alfred A.Commented:
You can use CustomValidator Control if you want.   You can use it for client-side validation.  Check the sample code below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
     <title>Validate Text Box</title>
     <script language="javascript" type="text/javascript">
          function ValidateTextBox(source, arguments) {
             var data = arguments.Value.split('');
             //start by setting false
             arguments.IsValid = false;
             //check length
             if(data.length != 8) return;
             arguments.IsValid = true;
          }
     </script>
</head>
<body style="font-family: Arial">
    <form id="form1" runat="server">
    <div>
         <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
         <asp:CustomValidator ID="txtTestCustomValidator" runat="server"
                Text="*" ToolTip="Only 8 digits allowed."
                ErrorMessage="Only 8 digits allowed."
                ControlToValidate="txtTest"
                ClientValidationFunction="ValidateTextBox"></asp:CustomValidator>
         <asp:Button ID="ButtonSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
hosneylkCommented:
Use regex to match..

e.g.


input control
<input type="text" id="TextBox1" onblur="chkTxtVals();"/>

javascript
function chkTxtVals() {
	if (!document.getElementById('TextBox1').value.match(/\d{8}/))
		alert('value must be 8 digits');
}

Open in new window

0
 
hosneylkCommented:
if it's at least 8 digits then use this regex

/\d{8,}/

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
itsdivyaksCommented:
you can always set the maxlength property of the textbox to 8

if only javascript needs to be used:

if the string is passed to the javascript function, use
if (str.length > 8)
    return str.substring(0,8);

and add the attribute to the textbox for onkeyup or onchange event..or u can just assign the attribute "javascript:if(str.length>8) return str.substring(0,8);" for the onkeyup or onchange events...

0
 
minglelinchAuthor Commented:
I tried the follow code, the alert box never popup ......

<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="return chkVals();"  OnClick="btnSave_Click"/>  

function chkVals() {
         var txtvalue = document.getElementById("<%=txtCNum.ClientID%>").value;
         if (!txtvalue.match(/\d{8}/)) {
                     alert(CNum must be 8 digits for saving');
      return false;
         }
0
 
itsdivyaksCommented:
i see only two issues here
apostraphe inside alert is missing: alert('
and function ending flower bracket is mising: }
0
 
hosneylkCommented:
i agree that other than the missing ' everything seems to be ok.

btw if you want btnSave_Click to be fired on the server when the value is valid you might want to do it this way..
OnClientClick="if (!chkVals()) return false;"

Open in new window

0
 
minglelinchAuthor Commented:
I tried all of these. It's not working. Then I just checked str.length and give alert message in javascript function, and it is working. Thanks for all the comments.
0
 
hosneylkCommented:
So you need 8 characters and not digits? A "digit" would be a numeral character. If your input doesn't have to be restrained to digits then sure you can use length.
0
 
minglelinchAuthor Commented:
I have restricted any character input at KeyPress event, only allowed digits entry, and then restricted input entry length to achive my purpose.
0
 
Tom KnowltonWeb developerCommented:
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
minglelinchAuthor Commented:
Those comments are very good. I would like to have them visible to me for future references.
0
 
minglelinchAuthor Commented:
THANKS.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now