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.
LVL 1
minglelinchAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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

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
hosneylkCommented:
if it's at least 8 digits then use this regex

/\d{8,}/

Open in new window

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
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.