regular expression validation in asp.net - asp.net

i am using Regular expression validator c# in asp.net for a login page..i have different login pages for student,lecturer and admin..the login ids are of the form 1RNxxCSxxx,1RNLECSxxx,1RNADCSxxx respectively(x-digits) my problem is it validates the text box and displays the error message.. but it still continues n logs in..my code is..
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Enter in 1RNxxCSxxx format" ValidationExpression="[1][R][N][0-9][0-9][C][S][0-9][0-9][0-9]"></asp:RegularExpressionValidator>
ie..if i type lecturer id in student login page i can still login inspite of gettin error message..any help would be appreciated.thank u

It seems from the name of your textbox that you're using custom login logic. I would suggest you look into using a membership provider which will be more robust in securing your entire application. Check out How to configure member ship with a database other than aspnetdb
For your immediate problem though in the method that logs the user in check if the page is valid.
if( !Page.IsValid )
{
return;
}
More information can be found here: this link
Also your regular expression is incorrect. Use the following for a successful match.
^1RN([0-9]{2}|LE|AD)CS[0-9]{3}$
The expression above says that the string should start (^ signifies a start) with 1RN have two digits or LE or AD followed by the letters CS and three digits ($ signifies end).

Related

How to create a session to display a users Login name

I am a bit of a ASP.NET/VB noobie so sorry in advance for any stupid questions.
I am currently creating a ASP.NET application using VB. The application I have had is connected to a local Database and I am able redirect a user to a "Users" page once they have input their login credentials correctly.
What I want to happen is that once they login and are redirected to the "Users" page, there will be a message saying "Welcome" followed by their login name.
I have been able to successfully do this in C# but need to be able to do so in VB as well.
The code that worked in C# is as follows;
**Session["New"] = TextBoxUserName.Text;
Response.Write("Password is correct");
Response.Redirect("Users.aspx");**
I've tried putting this code straight into my VB project but have received the following error;
"Property access must assign to the property or use its value"
I've done some research into this error and can't seem to find any solution to the problem.
Is my syntax incorrect or are sessions used completely different in VB?
I haven't seen your VB Code but it should be:
Session("New") = TextBoxUserName.Text
''Response.Write("Password is correct"); - No point in having this line as the redirect overrides it quickly.
Response.Redirect("Users.aspx")
Note the use of round brackets rather than square brackets for VB, also note the lackof semi colons. VB.NET and C# are completely different syntax wise.

Adding IP Address to Email Validation RegEx

I am using the RegEx "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+[.])*(\.[a-zA-Z]{2,17})$"to validate Email but my lead want to validate as per the Microsoft standard. SO i need to follow
http://msdn.microsoft.com/en-us/library/01escwtf(v=vs.100).aspx
In that everything working fine as per the standard but still i am facing the issues with
Valid: js#internal#proseware.com
Valid: j_9#[129.126.118.1]
the above mentioned mail ID is still returning as invalid. I tried using the regex used in that page
^(?("")(""[^""]+?""#)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])#))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$
but i am getting the error in the server page. Though I pasted the expression inside the validation Expression it can't able to accept the characters.
Note : am using ASP.Net validators for validating the email.
Description
To match both of those email addresses in your sample text, I think I would rewrite your expression like this:
[A-Z0-9._%#+-]+#(?:[A-Z0-9.-]+\.[A-Z]{2,4}|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])
If you're looking to use this to validate a string which may contain only an email then you can add the start/end of string anchors ^ and $.
Example
Live Demo
Sample Text
Valid: js#internal#proseware.com Valid: j_9#[129.126.118.1]
Matches
[0][0] = js#internal#proseware.com
[1][0] = j_9#[129.126.118.1]

How to check in an asp text box has # sign

I'm developing a module where I need to take input from a user one of each is users email. In order to filter out a wrong input I want to check if a corresponding checkbox has an # sign.
Does anyone knows how can I check it using vb.net ?
Aside the main question to check if a string contains the # character and considering that you are getting your string from a user input, in your case I think is better to check if the user entered a valid mail address.
To do so there are some solutions all reported in this other SO question How do I validate email address formatting with the .NET Framework?
In ASP.NET you can use RegularExpressionValidator for this.
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server"
ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="tbEmail"
ErrorMessage="Invalid Email Format">
</asp:RegularExpressionValidator>

only two digits allowed after decimal asp.net

I have a textbox where the user must not be able to enter more than two digits after a decimal.How do I do this without using javascript?
Thanks.
You can set the MaxLength property of the textbox, but that doesn't have any notion of whether or where the decimal point is.
You could also use a CustomValidator and check the inputted number on the server via the ServerValidate event. But this will require going to the server to check the value (i.e. it will initially look like your form allows users to input invalid numbers).
You should also be able use to a RegularExpressionValidator, depending on your exact globalization requirements, which will use JavaScript on the client to provide immediate feedback:
<asp:TextBox ID="NumberTextBox" runat="server" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="NumberTextBox"
ValidationExpression="\d+(?:(?:\.|,)\d{1,2})?" />
If you want the immediate feedback to the user, you'll need to use a JavaScript based solution.
You cannot cause the textbox to stop accepting text after two decimal places without directly or indirectly using javascript. (This is sometimes called an input mask).
You can, however, allow the user to enter free-form text and validate the text upon postback on the server. You can either automatically round the number for them, or return an error message to the client.
If you really need to prevent the user from entering more than two digits after the decimal point, you'll need to use JavaScript or a server control that implements the JavaScript for you.
However, may make more sense to allow them to enter any number of digits and then catch it on validation (or just round to two digits).

ASP.NET email validator regex

Does anyone know what the regex used by the email validator in ASP.NET is?
Here is the regex for the Internet Email Address using the RegularExpressionValidator in .NET
\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
By the way if you put a RegularExpressionValidator on the page and go to the design view there is a ValidationExpression field that you can use to choose from a list of expressions provided by .NET. Once you choose the expression you want there is a Validation expression: textbox that holds the regex used for the validator
I don't validate email address format anymore (Ok I check to make sure there is an at sign and a period after that). The reason for this is what says the correctly formatted address is even their email? You should be sending them an email and asking them to click a link or verify a code. This is the only real way to validate an email address is valid and that a person is actually able to recieve email.
E-mail addresses are very difficult to verify correctly with a mere regex. Here is a pretty scary regex that supposedly implements RFC822, chapter 6, the specification of valid e-mail addresses.
Not really an answer, but maybe related to what you're trying to accomplish.
We can use RegularExpressionValidator to validate email address format. You need to specify the regular expression in ValidationExpression property of RegularExpressionValidator. So it will look like
<asp:RegularExpressionValidator ID="validateEmail"
runat="server" ErrorMessage="Invalid email."
ControlToValidate="txtEmail"
ValidationExpression="^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$" />
Also in event handler of button or link you need to check !Page.IsValid.
Check sample code here : sample code
Also if you don't want to use RegularExpressionValidator you can write simple validate method and in that method usinf RegEx class of System.Text.RegularExpressions namespace.
Check example:
example
For regex, I first look at this web site: RegExLib.com
Apart from the client side validation with a Validator, I also recommend doing server side validation as well.
bool isValidEmail(string input)
{
try
{
var email = new System.Net.Mail.MailAddress(input);
return true;
}
catch
{
return false;
}
}

Resources