Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

prasad
More...


Forums » .NET » ASP.NET »

Javascript to enter only digits with "-"(eg:040-4444444)


Posted Date: 17 Jul 2012      Posted By:: Naseer     Member Level: Bronze    Member Rank: 2055     Points: 5   Responses: 5



i my page i want to enter the landline no,but i want to enter the district code so i want to enter in this format(eg:040-4444444),its taking only digits but i want to enter "-" also

<script type="text/javascript">
function AllowNumsOnly(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else
return true;
}
</script>



<asp:TextBox ID="txt_Qty" runat="server" onkeypress="return AllowNumsOnly(event);"></asp:TextBox>



thanks in advance,
Naseer.




Responses

#680725    Author: jogesh      Member Level: Gold      Member Rank: 229     Date: 17/Jul/2012   Rating: 2 out of 52 out of 5     Points: 2

try this...

here i have used regular expression, instead of ASCII keys

<script type="text/javascript">
function checkPhone(str)
{
var phone2 = ^[\d-]+$;
if (str.match(phone2)) {
return true;
} else {
return false;
}
}

</script>

Hope this may help you...



 
#680726    Author: Paritosh Mohapatra      Member Level: Diamond      Member Rank: 6     Date: 17/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Please check the following code:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function AllowNumsOnly(evt) {
var txt = document.getElementById('txt_Qty');
if (txt.value.length == 3) {
txt.value = txt.value + '-';
}
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
else
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt_Qty" runat="server" onkeypress="return AllowNumsOnly(event);"></asp:TextBox>
</div>
</form>
</body>
</html>



Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM



 
#680731    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 17/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Try like this code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Javascript validations</title>
<script language="JavaScript" type="text/javascript">
function NumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 47 && charCode < 58 || charCode == 127 || charCode == 8 || charCode == 45) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter number<asp:TextBox ID="TextBox5" runat="server" onkeyPress="return NumberKey(event);"></asp:TextBox>
</div>
</form>
</body>
</html>


Regards
N.Ravindran
Your Hard work never fails






 
#680742    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 19     Date: 17/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

You can do that by extending your code. But then the problem is user will be free to add any number of "hyphens (-)".
Though you can restrict user to enter only one "hyphen (-)", but then the problem is:
Suppose user enters a "hyphen (-)", then he/she found some mistake and used "backspace" key to delete what is already entered, then he/she cannot be able to enter "hyphen (-)" again, because you cannot trap the "backspace" key on "onkeypress" event.

So a work around of this if you want to add a hyphen between code and the number is, you can add it when you process the data.
But in this case also you have to keep track of the city, so that you know the length of the STD code (as it can be 3, 4, 5).

Hope it'll help you.
Regards
Ajatshatru



 
#680789    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 17/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Try using the below sample Query to validate the input as


<SCRIPT language="JavaScript">
<!--

function IsNumeric(strString)
// check for valid numeric strings
{
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}

// -->
</SCRIPT>


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Access masterpage label in content page
Previous : How to one button event check multiple button events in asp.net?
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.