How to validate ID number in asp.net? - asp.net

I am new at this so be easy on me... :)
I need to validate if a the ID number that the user typed in my site is a valid ID.
How do I check it?
Do I need to use RegularExpressionValidator?
More over, I also need to validate the credit card number, I found a few RegularExpressions for that in the net but each one is different from the other and I am not sure which one to use..
Does anyone know od a working expression that will suit all credit cards??
Thanks,
Tsil.

You never need to use the RegexValidator, it's just handy sometimes.
EDIT: a Regex can only check for a string-pattern and can't do any calculations or other checks that require numbers as opposed to "sequences of digits".
It all depends on what you consider a valid "ID". Is any number "valid" or are there more rules?
For a creditcard number you also need to specify what you consider valid: just a number of digits (12 I think?) with maybe added dots or spaces? Or do you want to know whether that card itself is valid? That's a different problem entirely!
EDIT: for a SSN, see wikipedia.

What kind of ID are you talking about? A social security number or a User ID to login?
Since you are talking about it in the context of a Credit card and Regular Expression validators, I will assume that you are talking about a Social Security number.
Anyways, if you Are talking about Login controls or such please go here.
Regarding Social Security number, this depends on what country the person is from and how the individual country sets up their numbers, but to give you a little taste of what you might want to do, check out How to use Regulat Expression Validators.
However, Please, please, please do not validate the credit card number yourself. There are Bank Services that you can use for this, head over to PayPal and check their API for C#, this will most certainly make your users feel more comfortable on your site.
Also use SSL.
Regular expression validator example
<%# language="C#" %>
<form id="form1" runat="server">
<asp:TextBox ID="txtName" runat="server"/>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:RegularExpressionValidator ID="regexpName" runat="server"
ErrorMessage="This expression does not validate."
ControlToValidate="txtName"
ValidationExpression="^[a-zA-Z'.\s]{1,40}$" />
</form>
If you want to try out some regular expressions head over to the greatest resource there is. And don't forget to read this.
You might also want to read User Input validation in ASP.NET from msdn and Learn ASP.NET from Microsoft has some great videos on ASP.NET Security that you might find usefull.

Related

client id mode on 2 identical id's

I have an control1.ascx page
and control2.ascx page
both of them have this element:
<asp:TextBox runat="server" ID="txt_name" ClientIDMode="Static"></asp:TextBox>
page.aspx contains both of the controls.
so now the page contains input type text with the id "txt_name" X2.
I am wondering how it is working? can someone explain?
Ideally, you want to use ClientIDMode="Static" only if you are sure that no other control has same name in the page.
For example, you really want to access the ServerControl from external javascript file (althought it is not a good design).
If you are not sure, you want to use Predictable.
As your are using ClientIDMode="Static" thus control's id will be rendered exactly as it is.
ID will be rendered directly. See MSDN docs, this Blog is a good read.
ASP.Net 4+ supports various modes to generate ClientIDs for controls.
Here is a reference for MSDN on ClientID and its' generation modes: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid(v=vs.100).aspx
To see how ClientID generation works, you may want to refer to this link :
http://www.codeproject.com/Articles/108887/Client-Ids-Generation-with-ASP-NET-4-0
(It has visual explanations on how the ClientID generation works in different cases)
Hope this helps.

EnableViewState="false" does not work and Why asp.net view sate automatically decoded and stored in browser

I used asp.net text-box and set
EnableViewState="false"
then i run my code and enter some sample texts and i enforced the post-back (which means click the button )then Textbox control retain the value .
what i am wrong in my code ?
How can i disable view-state ?
<asp:TextBox ID="TextBox1" EnableViewState="false" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Then i have another one doubt.
Why asp.net view sate automatically decoded and stored in browser. I read some articles the article says it’s a security purpose.
The user gives her/his information and he will use the particular browser and maintain the browser then why view sate is encoded. Is another reasons to decode the view state ?
Well regarding your first question this can be confusing at the beginning. Textbox are simply classes that implement the IPostBackDataHandler interface.
A nice explation can be found here-- http://www.codeproject.com/Articles/378180/View-State-for-TextBox-and-other-controls-that-imp
Regarding your second question about the encryption of viewstate, then you must know the user accessing the page is not only one who can view the viewstate. Pages are posted back on un encrypted channels also, so any body looking over the wire has access to it. Also the user can never be trusted.
My advice to you is to get in details about the view state on msdn. It will help you in long run.

Commentbox instead of text box?

I want the user to be able to leave comments, a few sentences perhaps. TextBox is to no use, but I see no asp:CommentBox
Any advice on what to use instead?
Thanks
Set property TextMode to MultiLine
As this
<asp:TextBox ID="Text1" runat="server" TextMode="MultiLine" />
You can see more on MSDN http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textmode.aspx
If the comments part of your project is so important i suggest using third-parties for these such functions
check these 2 sites
Disqus Elevating the discussion, anywhere on the web.
livefyre WE MAKE YOUR SITE SOCIAL.
i know that's not answer to your question, but i think you should at least know they are exist.

Globalization difference in inline tags in ASP.NET

Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:
<%=Resources.Site.SampleString %>
The resources tag (expression tag) as seen in any MSDN example:
<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />
I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?
These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.
By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:
<asp:Literal id="Literal1" runat="server"
meta:resourcekey="SampleString" text="Default one" />
and it all should work exactly the same.
EDIT on implicit Localization.
What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:
Values are taken from App_LocalResources, therefore related resource file need to exist
Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.
I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).
So if you need or want designer support, the second option would be necessary.

Can an ASP.NET Validation Rule belong to multiple groups?

Is it possible for an ASP.NET Validation Rule to belong to multiple groups? I'd like to validate the same control in different ways based on what mode the form is in using a single Validator.
For the purposes of this question, the modes are Simple, which requires fewer fields, or Complex, which requires more fields. I know I can write CustomValidators (which I have done in the past), but I'd like a simpler solution.
So, I'd like to combine the following:
<asp:RequiredFieldValidator ValidationGroup="Simple" ControlToValidate="Name" />
<asp:RequiredFieldValidator ValidationGroup="Complex" ControlToValidate="Name" />
Thanks!
No, unfortunately they cannot. But to my understanding your page is never shown in simple and complex mode at the same time, so maybe you should just add the validator from codebehind and set the validationgroup based on the current mode.
There is no support for multiple validation groups on a validator or button in the ASP.NET Validators

Resources