• http://www.techneurons.com/career/
  • experienced programming consultants for hire !!!

    Contact Now

    ASP.NET Support

    Phone Validation in ASP.NET

    Posted by Charul Awal on Mar 21, 2010
    Hi,

    I have a format phone number like this : 443XXXXXXXX. I want to implement this to my program so the people who register have a default format like that. can anyone help me how to generate this validation and implement that with asp.net.

    thanks
    Replied by consultsarath on Mar 22, 2010
    <p>You can implement this validation using a simple Regular Expression. This can be applied either on client side, or on server side. But this would be better if the validation is done on client side, attached to the o<strong>nblur </strong>event. This would keep the user on the textbox until he enters the right formatted input.</p> <p>The regular expression for this purpose would be: <strong>^443[0-9]{8}$</strong></p> <p>You can use a simple javascript function:</p> <p>&nbsp;</p> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; color: blue; font-size: 9pt">function</span><span style="font-family: 'Courier New'; font-size: 9pt"> blurPhoneCheck(cc) {</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (cc.value == <span style="color: #a31515">''</span>)</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">true</span>;</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> rex = /^443[0-9]{8}$/</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; <span style="color: blue">if</span> (rex.test(cc.value)) {</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">true</span>;</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; }</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; alert(<span style="color: #a31515">'You have entered an invalid phone number. Please correct this to proceed.'</span>);</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; cc.focus();</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; cc.select();</span></div> <div style="line-height: normal; margin: 0cm 0cm 0pt 36pt"><span style="font-family: 'Courier New'; font-size: 9pt">&nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">false</span>;</span></div> <div style="margin: 0cm 0cm 10pt 36pt"><span style="line-height: 115%; font-family: 'Courier New'; font-size: 9pt">}</span></div> <p>&nbsp;On the ASP.NET Source view, you can atttach a simple attribute:</p> <div style="margin: 0cm 0cm 10pt"><span style="line-height: 115%; font-family: 'Courier New'; color: blue; font-size: 9pt">&lt;</span><span style="line-height: 115%; font-family: 'Courier New'; color: #a31515; font-size: 9pt">asp</span><span style="line-height: 115%; font-family: 'Courier New'; color: blue; font-size: 9pt">:</span><span style="line-height: 115%; font-family: 'Courier New'; color: #a31515; font-size: 9pt">TextBox</span><span style="line-height: 115%; font-family: 'Courier New'; font-size: 9pt"> <span style="color: red">runat</span><span style="color: blue">=&quot;server&quot;</span> <span style="color: red">ID</span><span style="color: blue">=&quot;txtPhone&quot;</span> <span style="color: red">Width</span><span style="color: blue">=&quot;120px&quot;</span> <span style="color: red">MaxLength</span><span style="color: blue">=&quot;11&quot;</span> <span style="color: red">onblur</span><span style="color: blue">=&quot;return&nbsp;blurPhoneCheck(this)&quot;</span> <span style="color: blue">/&gt;</span></span></div>
    We are experts in Cloud Computing Technologies. We can assist you to build high scalable business applications using Amazon Web Services (Amazon EC2, Amazon S3, Amazon SES, SNS, CloudFront), Windows Azure Platforms - Windows Azure and SQL Server Azure, Google App Engine using Python and Django Framework. We are Expert Programming Consultants available at affordable rates per hour. We work on several technologies - .NET, Python, Google App Engine, PHP, Windows Azure, Amazon Web Services ...