Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts
this.carriers = this.carriers.filter((el, i, a) => i === a.indexOf(el))
atob(): Decodes a string of data which has been encoded using base-64 encoding.
btoa(): Creates a base-64 encoded ASCII string from a "string" of binary data.
The "Unicode Problem": In most browsers, calling btoa() on a Unicode string will cause a Character Out Of Range exception. This paragraph shows some solutions.
Solution to unicode
btoa(): Creates a base-64 encoded ASCII string from a "string" of binary data.
The "Unicode Problem": In most browsers, calling btoa() on a Unicode string will cause a Character Out Of Range exception. This paragraph shows some solutions.
Solution to unicode
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [2, 4];
var arr = arr1.filter(x => { return arr2.indexOf(x) === -1; });
public search(name: string): void {
if (name.trim().length > 0) {
this.employees = this.employeeSource.filter(x => x.name.toLowerCase().indexOf(name.trim().toLowerCase()) >= 0);
}
}
public getTotalWeight(): number {
var total = 0;
if (this.items != null && this.items.length > 0) {
this.items.forEach(x => total += x.weight);
}
return total;
}
ASP.NET Code
<asp:RadioButtonList runat="server" ID="radReportType" onchange="displayReportType();" RepeatDirection="Horizontal">
<asp:ListItem Text="Shipment" Value="Fruit" Selected="True">
<asp:ListItem Text="Customer" Value="Vegetable">
<asp:ListItem Text="Carrier" Value="Meat">
</asp:RadioButtonList>
jQuery
var radio = $("[id*=radFruits] input:checked");
var value = radio.val();
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:
- Merge the value into single line of code by replacing the end of line character with empty string
- Check if the value is number using regular expression
function isNumber(value){
var pattern = new RegExp("^[0-9]+$");
value = value.replace(/\n/g,"");
return pattern.test(value);
}
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>
Following are example of JavaScript Validation Function and useful patterns.
JavaScript Function
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+))*$");