function IsNumeric(sText) {
if (sText == null) return false;
var ValidChars = "0123456789";
var IsNumber = true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++) {
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1) {
IsNumber = false;
}
}
return IsNumber;
}
Randomly shuffle a list of items in C#
public static List<T> DesordenarLista<T>(List<T> input)
{
List<T> arr = input;
List<T> arrDes = new List<T>();
Random randNum = new Random();
while (arr.Count > 0)
{
int val = randNum.Next(0, arr.Count - 1);
arrDes.Add(arr[val]);
arr.RemoveAt(val);
}
return arrDes;
}
Validate URL with C#
public static bool ValidateUrl(string url)
{
if (url == null || url == "") return false;
Regex oRegExp = new Regex(@"(http|ftp|https)://([\w-]+\.)+(/[\w- ./?%&=]*)?", RegexOptions.IgnoreCase);
return oRegExp.Match(url).Success;
}
Validate an email with C#
public static bool ValidateEMail(string email)
{
if (email == null || email == "") return false;
Regex oRegExp = new Regex(@"^[A-Za-z0-9_.\-]+@[A-Za-z0-9_\-]+\.([A-Za-z0-9_\-]+\.)*[A-Za-z][A-Za-z]+$", RegexOptions.IgnoreCase);
return oRegExp.Match(email).Success;
}
Devolver un código 301 (Moved Permanently) en C#
En algunos casos necesitamos informar a terceros que una página nuestra, o incluso un dominio entero ha sido movido a otro sitio.
Un ejemplo claro podría ser que teníamos el dominio www.newagedesign.com y ahora lo hemos movido a www.newagedesign.es. Lo ideal es avisar a todo el mundo que la dirección de nuestra web ha cambiado, pero esto a veces no es posible o puede llevar mucho tiempo. En estos casos lo ideal es redireccionar al visitante pero indicándole que le redirigimos porque la web ha cambiado de lugar. Esto se consigue devolviéndole un error 301.
Todo esto comentado sobre un dominio también es aplicable a una página concreta.
En el caso de .NET esto se haría insertando el siguiente código en la página que queremos redirigir:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.newagedesign.es/");
Response.End();
Codificar un string a hexadecimal en JavaScript
En cierto momento necesité codificar una cadena de texto en su representación en hexadecimal con la finalidad de realizar una tareas de codificación. La función JavaScript que hace esto es la siguiente:
function encodeToHex(str) {
var result = "";
for (var i = 0; i < str.length; i++) {
result += str.charCodeAt(i).toString(16);
}
return result;
}