Showing posts with label Validation. Show all posts
Showing posts with label Validation. Show all posts

Asp.net POST Form Data to Another Page

Posted by Unknown On Thursday, January 22, 2015 0 comments
Problem: I have a login form written in Asp.net code. I want to post my username and password to authentication.aspx page to verify this user, but I cannot use action link in form because it is used by other button too.

Solutions: use postbackurl property in Asp.net Button.

ASP.NET Page
<asp:Button OnClientClick="if(!validateLogin()) return false;" 
     PostBackUrl="~/Account/authentication.aspx" Text="Sign in" runat="server" ID="butLogin" />

Note: if you want to validate the form before posting data, you need to do the same way ask I do. It won't work if you use:
OnClientClick="return validateLogin();"

JavaScript Multiline Validation with Regular expression

Posted by Unknown On Tuesday, January 6, 2015 0 comments

How to validate multiple line values using JavaScript and Regular Expression.
Problem: I want to validate if my textarea contains only number. But my textarea accepts multiple Number values separated by break line.
1234
6743
2934
2134

Solutions:

  1. Merge the value into single line of code by replacing the end of line character with empty string
  2. Check if the value is number using regular expression

JavaScript
function isNumber(value){
   var pattern = new RegExp("^[0-9]+$");
   value = value.replace(/\n/g,"");
   return pattern.test(value);
}

Client Size validation with ASP.NET CustomValidator

Posted by Unknown On Monday, January 5, 2015 0 comments

Using JavaScript to custom validate asp.net control.

.ASPX File & JavaScript
function ValidateAge(source, arguments){
    var pattern = new RegExp("^[0-9]{2}$");
    if (!pattern.test(arguments.Value)){
         source.errormessage ="Age must be numeric";
         arguments.IsValid = false;
    }else{
         arguments.IsValid = true;
    }
}

<asp:CustomValidator 
     id="valCustom" 
     ControlToValidate="txbAge"
     ClientValidationFunction="ValidateAge"
     OnServerValidate="ValidateAge"
     ErrorMessage="This can be set from code"
     ForeColor="red" 
     runat="server">
     ValidationGroup="valSave"
</asp:CustomValidator>

Javascript Custom Regular Expression Validation

Posted by Unknown On 0 comments
Following are example of JavaScript Validation Function and useful patterns.

JavaScript Function
function ClientValidate(str) {
    var pattern = new RegExp("^[a-zA-Z0-9]+$"); 
    return pattern.test(arguments.Value);
}
Any characters and number. No special characters.
var pattern = new RegExp("^[a-zA-Z0-9]+$");
Numeric number only
var pattern = new RegExp("^[0-9]+$");
Email Validataion
var pattern = new RegExp("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
//ex. matinee.la@mymail.office
//ex. matinee.la@yahoo.com
Phone Number
var pattern = new RegExp("^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$");