Skip to main content

Posts

Showing posts from 2020

How to convert JavaScript Date without time stamp

 At some point when setting pika date min date I had to use JavaScript date variable without time stamp. When you try JavaScript new Date() in browser console, you can see date with time stamp. To remove time stamp what I did was use new Date with year , month and day parameters. new Date(new Date().getFullYear(),new Date().getMonth() , new Date().getDate())

.Net C# match a UK postcode with a regular expression

UK postcode Validation in C# [ RegularExpression (@" ^(([a-zA-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][a-zA-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[a-zA-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$ ", ErrorMessage = "Invalid Post Code.")] public string PostCode { get; set; } Refer -  https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom

Setting attributes of a property in partial classes / Is it possible to add an attribute to a property in a partial class?

Question I have an employee class generated by the  Entity Framework  (EF). public partial class employee { private string name; public string Name { get{return name;} set{ name = value;} } } Now I want to put a required attribute in the name property to use in for MVC3 validation in another employee partial class which is written by me in order to extend the one which is generated by EF so that I don't have to rewrite my code if I refresh the model generated by EF. My written partial class is in the same assembly and namespace. public partial class employee { // What should I write here to add required attribute in the Name property? } Answer It is actually possible only through buddy class but it is not recommended way. You should keep your validation in the custom view model because often you need different validations for different views but your entity can keep only a single set of validation attributes. Example of