MVC4 with Entity Framework Invalid entry for EnrollmentDate - asp.net

I'm following a tutorial from here
For some reason when I try to create a new user with a date it won't accept it unless the month is January between dates ranging from 1-12ish.
I'm pretty sure it's because of the ValidationMessageFor(in the User.cs) method which forces me to enter a date which month must be January and I don't know where to alter it.
jquery.validate
jquery.validate.unobtrusive

Add code into script
$.validator.addMethod('date', function (value, element) {
if (this.optional(element)) {
return true;
}
var valid = true;
try {
$.datepicker.parseDate('dd/mm/yy', value);
}
catch (err) {
valid = false;
}
return valid;
});
$('#dt1').datepicker({ dateFormat: 'dd/mm/yy' });

Related

2sxc Allow changes after formula filled a value

I'm trying to create a field that automatically fills a date (or whatever value).
The formula works but it also locks the value from any changes the user attempts.
I've tryed this:
v1(data, context) {
return new Date();
}
and this:
v1(data, context) {
if (context.cache.alreadyRun) return new Date();
context.cache.alreadyRun = true;
}
but the value always gets locked in the form.
The logic is wrong. Once it has run, it shouldn't return the date any more, but just continue with the existing value.
It should be more like this:
v1(data, context) {
if (context.cache.alreadyRun) return data.value;
context.cache.alreadyRun = true;
return new Date();
}
Note that I believe it's still wrong - you probably just want to set the date when it's empty, and not set it again every time the form is opened. So this is probably what you're looking for:
v1(data, context) {
if (data.value) return data.value;
return new Date();
}

ServiceNow: Calling onSubmit in a callback function leads to infinite loop

Here I am comparing 2 variables for dates start_date and end_date and allowing to submit the form only in case end_date is bigger than start_date, else rejecting the form to be submitted, but while running this code, it goes into the infinite loop and if i make this asynchronous by using getXMLWait() instead of getXML(checkDateDiff) it's not supported with mobile api's.
Also there are lot of client script which help in comparing dates but none of them is supported with mobile apis.
Please have a look at the below code and help!!!!
function onSubmit() {
var requestType = g_form.getValue('request_type');
if (requestType == 'mifi') {
console.log("calling validateTravelEndDate()");
validateTravelEndDate();
return false;
} else
return true;
}
//Helper function which calls a AJAX script include called "ClientDateTimeUtils" which gives the response in a callback where i am deciding whether to submit the form or not based on the status of days result.
function validateTravelEndDate() {
var startDate = g_form.getValue('travel_start'); //First Date/Time field
var endDate = g_form.getValue('travel_end'); //Second Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
console.log("startDate :" + startDate + "endDate :" + endDate);
var ajax = new GlideAjax('ClientDateTimeUtils'); // This is the script include which can be used for date validation.
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', startDate);
ajax.addParam('sysparm_sdt', endDate);
ajax.addParam('sysparm_difftype', dttype);
console.log("before " + g_form.getValue('travel_end'));
ajax.getXML(checkDateDiff);
}
// callback function where deciding to go ahead or not with form submission.
function checkDateDiff(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("difference in days:" + answer);
if (answer <= 0) {
alert("Travel End date must be after Travel Start date.");
g_form.setValue('travel_end', '');
g_form.showFieldMsg('travel_end', 'Please provide a future date', 'error');
return false;
} else {
console.log("%%%%%%%%%%%%%%% Calling g_form.submit()");
g_form.submit(); // This has some issue as it’s going in the infinite loop and if we just return true/false from here as it’s asynchronous call , it’s not handled by the onSubmit function
}
}
Your onSubmit() function always returns false for a mifi request. onSubmit() functions can execute a safer submit when they return a true. Also, g_form functions cannot be run in the callback function, since that is executed on the server.
Rather than have a g_form.submit() at the end of your checkDateDiff function, have onSubmit() function return true.
Something like this should work. I commented every line that I changed:
function onSubmit() {
var requestType = g_form.getValue('request_type');
if (requestType == 'mifi') {
console.log("calling validateTravelEndDate()");
// **CHANGED CODE: instead of g_form.submit(), this will return true
if(validateTravelEndDate()){
return true;
}
else{
return false;
}
} else
return true;
}
//Helper function which calls a AJAX script include called "ClientDateTimeUtils" which gives the response in a callback where i am deciding whether to submit the form or not based on the status of days result.
function validateTravelEndDate() {
var startDate = g_form.getValue('travel_start'); //First Date/Time field
var endDate = g_form.getValue('travel_end'); //Second Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
console.log("startDate :" + startDate + "endDate :" + endDate);
var ajax = new GlideAjax('ClientDateTimeUtils'); // This is the script include which can be used for date validation.
ajax.addParam('sysparm_name', 'getDateTimeDiff');
ajax.addParam('sysparm_fdt', startDate);
ajax.addParam('sysparm_sdt', endDate);
ajax.addParam('sysparm_difftype', dttype);
console.log("before " + g_form.getValue('travel_end'));
// **CHANGED CODE: validateTravelEndDate returns the callback value
return ajax.getXML(checkDateDiff);
}
// callback function where deciding to go ahead or not with form submission.
function checkDateDiff(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("difference in days:" + answer);
if (answer <= 0) {
alert("Travel End date must be after Travel Start date.");
g_form.setValue('travel_end', '');
g_form.showFieldMsg('travel_end', 'Please provide a future date', 'error');
return false;
}
else {
// **CHANGED CODE: checkDateDiff will return true
return true;
}
}

javascript validation for name,contact numbers

I have created 2 pages login and signup..On signup form I have used javascript for nonempty fields. Now I want to add validation for characters only, digits and date.
How to write these additional validations in my function. Please help. My code is:
<script type="text/javascript">
function Validateform() {
var Firstname = document.getElementById("txtfirst").value;
if (Firstname == "First Name" || Firstname == ""){
alert("Firstname must be filled out");
return false;
}
}
</script>
and I have called this function on onClientClick event of signup button.
Code to check alpha characters:
function onlyAlpha(inputtext){
var acceptedChars= /^[A-Za-z]+$/;
if(inputtext.value.match(acceptedChars)){
alert('ok');
return true;
} else {
alert('input alphabet characters only');
return false;
}
}
you can call this function on onblur on onClick with inputtext parameter as u wish to validate and the same way u can apply digits validation too.
There are n number of methods to validate name. Let me share my view
Method 1
First Name:
lname=document.getElementById('lname').value;
namelen=/^([a-zA-Z0-9]{5,15})+$/;
if(!namelen.test(lname))
{
alert('Enter a name');
return false;
}
else
{
document.getElementById('splname').innerHTML="";
}
returnValue;
}
Method 2 - To color the background
namelen=document.getElementById('lname').value.length;
lname=document.getElementById('lname').value;
if((namelen=<5 || namelen >=12) || !isNaN(lname))
{
document.getElementById('splname').innerHTML="Enter a name";
document.getElementById('lname').style.background="red";
document.getElementById('lname').focus();
}
else
{
document.getElementById('splname').innerHTML="";
document.getElementById('lname').style.background="green";
}
returnValue;
}
Method 3
fname=document.getElementById('fname').value;
namelen=/^([a-zA-Z0-9]{5,15})+$/;
if(!namelen.test(fname))
{
document.getElementById('spfname').innerHTML="Enter a name";
return false;
}
else
{
document.getElementById('spfname').innerHTML="";
}

Checking if a ValidationGroup is valid from code-behind

Is there a method I can call that retrieves a boolean value of whether or not a particular ValidationGroup is valid? I don't want to actually display the validation message or summary - I just want to know whether it is valid or not.
Something like:
Page.IsValid("MyValidationGroup")
Have you tried using the Page.Validate(string) method? Based on the documentation, it looks like it may be what you want.
Page.Validate("MyValidationGroup");
if (Page.IsValid)
{
// your code here.
}
Note that the validators on the control that also caused the postback will also fire. Snip from the MSDN article...
The Validate method validates the
specified validation group. After
calling the Validate method on a
validation group, the IsValid method
will return true only if both the
specified validation group and the
validation group of the control that
caused the page to be posted to the
server are valid.
protected bool IsGroupValid(string sValidationGroup)
{
foreach (BaseValidator validator in Page.Validators)
{
if (validator.ValidationGroup == sValidationGroup)
{
bool fValid = validator.IsValid;
if (fValid)
{
validator.Validate();
fValid = validator.IsValid;
validator.IsValid = true;
}
if (!fValid)
return false;
}
}
return true;
}
var isValidGroup = Page
.GetValidators(sValidationGroup)
.Cast<IValidator>()
.All(x => x.IsValid);
Try this:
Page.Validate("MyValidationGroup");
if (Page.IsValid)
{
//Continue with your logic
}
else
{
//Display errors, hide controls, etc.
}
Not exactly what you want, but hopefully close.
Page.IsValid will be false if any of the validated validation groups was invalid. If you want to validate a group and see the status, try:
protected bool IsGroupValid(string sValidationGroup)
{
Page.Validate(sValidationGroup);
foreach (BaseValidator validator in Page.GetValidators(sValidationGroup))
{
if (!validator.IsValid)
{
return false;
}
}
return true;
}
Pavel's answer works but isn't the simplest. Here is how I solved it:
protected Boolean validateGroup(String validationGroupName) {
Boolean isGroupValid = true;
foreach (BaseValidator validatorControl in Page.GetValidators(validationGroupName)) {
validatorControl.Validate();
if (!validatorControl.IsValid)
isGroupValid = false;
}
if (!isGroupValid)
return false;
else
return true;
}

How to combine similar JavaScript methods to one

I have an ASP.NET code-behind page linking several checkboxes to JavaScript methods. I want to make only one JavaScript method to handle them all since they are the same logic, how would I do this?
Code behind page load:
checkBoxShowPrices.Attributes.Add("onclick", "return checkBoxShowPrices_click(event);");
checkBoxShowInventory.Attributes.Add("onclick", "return checkBoxShowInventory_click(event);");
ASPX page JavaScript; obviously they all do the same thing for their assigned checkbox, but I'm thinking this can be reduced to one method:
function checkBoxShowPrices_click(e) {
if (_hasChanged) {
confirm(
'All changes will be lost. Do you wish to continue?',
function(arg) {
if (arg.toUpperCase() == 'YES') {
var checkBox = document.getElementById('<%=checkBoxShowPrices.UniqueID%
>');
checkBox.checked = !checkBox.checked;
eval("<%=base.GetPostBackEventReference(checkBoxShowPrices)%>");
_hasChanged = false;
}
});
return false;
} else {
eval("<%=base.GetPostBackEventReference(checkBoxShowPrices)%>");
}
}
function checkBoxShowInventory_click(e) {
if (_hasChanged) {
confirm(
'All changes will be lost. Do you wish to continue?',
function(arg) {
if (arg.toUpperCase() == 'YES') {
var checkBox = document.getElementById('<%
=checkBoxShowInventory.UniqueID%>');
checkBox.checked = !checkBox.checked;
eval("<%=base.GetPostBackEventReference(checkBoxShowInventory)%>");
_hasChanged = false;
}
});
return false;
} else {
eval("<%=base.GetPostBackEventReference(checkBoxShowInventory)%>");
}
}
Add to the event the checkbox that is raising it:
checkBoxShoPrices.Attributes.Add("onclick", "return checkBox_click(this, event);");
Afterwards in the function you declare it like this:
function checkBoxShowPrices_click(checkbox, e){ ...}
and you have in checkbox the instance you need
You can always write a function that returns a function:
function genF(x, y) {
return function(z) { return x+y*z; };
};
var f1 = genF(1,2);
var f2 = genF(2,3);
f1(5);
f2(5);
That might help in your case, I think. (Your code-paste is hard to read..)

Resources