Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2011, 10:43 PM
Authorized User
 
Join Date: Jun 2010
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to put validation on Textbox ?

Dear Imar Sir,
I have created a contact form in which there is a textbox for Mobile number
in india the mobile number is of 10 digit exactly failing which means mobile
number is incorrect. That is why i would like to put validations on textbox
so that when any user put less than 10 digit in mobile number textbox he got
alert message Mobile number is incorrect. How to achieve it through c# programming.
I think such option is not availabele in textbox property.
 
Old November 22nd, 2011, 03:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can use a CustomValidator or a RegularExpressionValidator to accomplish this. You'll need to Google a bit for the exact reg ex code.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old December 3rd, 2011, 01:58 AM
Registered User
 
Join Date: Nov 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm using javascript with regard to customValidator to verify textboxes within ASP.Internet. The actual code works completely when I 'm using a normal web page, but as soon as I put which in the MasterPage, the actual code fails.

Beneath is the code with regard to my aspx web page. Basically put this signal inside a MasterPage it doesn't function. Could you guys advise me steps to make this work in the MasterPage
The actual piece of software is:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

<script type="text/javascript">
function validateOrFields(source, args)
var sUser = document.getElementById('TextBox1');

if (sUser.value == "")

args.IsValid = false;

else

args.IsValid = true;

return;


<div>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:CustomValidator ID="FieldValidator"
runat="server"
Text="Enter either a user name"
ClientValidationFunction="validateOrFields" onservervalidate="FieldValidator_ServerValidate"/>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
 
Old December 3rd, 2011, 06:37 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

A master page changes the client side ID of your control. To fix the code, either update it by using the client ID of the control (look in the source for its new name) or use something like this instead:

'<%=Textbox1.ClientID%>'

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
 
Old December 15th, 2011, 07:53 AM
Registered User
 
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can also make your own custom validation function. This may be very naive, but somehow it will work.
private bool WithErrors()
{
if(textBox1.Text.Trim() == String.Empty)
return true; // Returns true if no input or only space is found
if(textBox2.Text.Trim() == String.Empty)
return true;
// Other textBoxes.

return false;
}

private void buttonSubmit_Click(object sender, EventArgs e)
{
if(WithErrors())
{
// Notify user for error.
}
else
{
// Do whatever here... Submit
}
}
The Following User Says Thank You to Colton For This Useful Post:





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to put validation for check box? Abhishek Kumar ASP.NET 3.5 Basics 3 December 16th, 2011 03:01 AM
Textbox validation rhd110 .NET Framework 2.0 1 March 28th, 2007 02:49 PM
How to put validation with Javascript in Firefox. nitinp ASP.NET 2.0 Professional 0 February 7th, 2007 05:15 AM
textbox validation in c# dhol C# 3 July 22nd, 2005 05:57 AM
validation on textbox noor ASP.NET 1.0 and 1.1 Basics 2 May 4th, 2005 02:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.