Page_ClientValidate is undefined in formview - javascript

I have validators inside a <asp:formview>, in order to show custom validation I am using
if (!Page_ClientValidate("groupName")) {}
I am getting an error Object Expected. How can I validate client side from the form view?
I use Page_ClientValidate for <asp:listview> and there

The Page_ClientValidate function may sometimes be undefined, e.g. if there are no validators on the page. Check if typeof Page_ClientValidate === "function" before calling it.

This would also occur if all validator's EnableClientScript properties are set to false.

Call the following Javascript function whenever you want and pass the validation group name of your form to it..
function ValidateForm(ValidationGroupName)
{
var validated=Page_ClientValidate(ValidationGroupName);
if(validated)
{
//do the logic here
return true;
}
else
{
return false;
}
}
Hope this will help you....

Related

C# Razor and JavaScript?

I need to check a boolean value coming from my .NET viewmodel in my JavaScript. If the user isn't signed in meaning my boolean of IsAnonymous would be true, I need to fire off two JavaScript functions. If it's false, I don't want to call the functions. I need to do this because one of the functions calls an ApplicationUser object which doesn't exist if the person using the website isn't signed in. And therefore, it throws a Null Reference Exception.
I've tried this code in my script tag at the bottom of my razor page, but the addBookmark function gets called even if Model.IsAnonymous returns true. Since addBookmark requires an ApplicationUser object, I'm throwing an error.
if (!#Model.IsAnonymous) {
if ($('.bookmark-btn').hasClass('bookmark-story-btn')) {
addBookmark();
} else {
removeBookmark();
}
}
Just do it at your View!
#if (!Model.IsAnonymous)
{
<script>
if ($('.bookmark-btn').hasClass('bookmark-story-btn')) {
addBookmark();
} else {
removeBookmark();
}
</script>
}

How does prototype work for onSubmit:function()?

I was working on Co-drops Minimal Form Interface. I couldn't understand this code snippet in stepsForm.js. (Line 50)
stepsForm.prototype.options = {
onSubmit : function() { return true; }
};
I am new to JS, and wouldn't mind an explanation of the entire code in stepsForm if anyone has the time to do so. But, for the time being, an explanation for the above can do wonders for me. I know what a prototype is, but the onSubmit part is going over my head. I read on another question that this is to prevent refresh, but I feel that is wrong.
The library exposes options property that you may/can use to pass your own overriding values.This one in particular, exposes onSubmit.
For any html form an onSubmit is called when the submit action is invoked by another function or by click.
In the library the default onSubmit is returning true, meaning just execute the action. This can be overriden with you custom function like this...
<script>
var FORM_ELEMENT = document.getElementById( 'myForm' )
new stepsForm(FORM_ELEMENT, {
onSubmit :
function (FORM_ELEMENT) {
alert('You are about to submit the form ');
//manipulate your form or do any preprocess work...
return true;
});
</script>
Within the library the _submit (line 196 stepForm.js) is called which inturn calls the onSubmit. This time, instead of the default, it will execute the one we added above.
stepsForm.prototype._submit = function() {
this.options.onSubmit(this.el);
}
Hope that helps.

confirm event within click event returned error

$('#reset').click(function(){
var confirm = confirm("This will reset everything, do you really want to continue?!");
if (confirm == true) {
alert();
}
});
Any idea why above code doesn't work? I got an error of undefined is not a function.
It is because of the local variable confirm.
Since you have declared a local variable with name confirm, when you use confirm() in your function it will have the value undefined as it is no longer referring to the global confirm function.
Just rename the variable and it should be fine.
$('#reset').click(function () {
var value = confirm("This will reset everything, do you really want to continue?!");
if (value == true) {
alert();
}
});
That's because after running this code once, confirm is not a function, but a boolean, in handler's function scope.
Change the local variable name to something else, like 'value' or 'confirmed'.
Confirm is a function, please change the variable confirm in to something else.
var result = confirm("This will reset everything, do you really want to continue?!");
You should try by changing the name of your variable "confirm" (var confirm). It is the keyword of javascript language.

Check if JQueryUI button exists before destroying?

Lets say I have the following code:
JS:
$('.remove').button("destroy");
If I were to run this before .button() is called, I get an error in the console like the following:
Error: cannot call methods on button prior to initialization; attempted to call method 'destroy'
How can I check to see if a button was actually created before attempting to destroy it?
$('.remove.ui-button').button("destroy");
You can try
$('.remove').filter(function(){
return $(this).data().uiButton != undefined
}).button('destroy')
You may use this -
if ($('.remove').hasClass("ui-button"))
{
// Button exists
}
else
{
// Button does not exists
}
I just figured it out
if($('.remove').is(":ui-button")){
$('.remove').button("destroy");
}
try to take advantage of "create" button
create a global variable
var created=false;
while applying ui to button
$('button').button({create:function(){created=true;}});
while removing
if(created==true)
{
// destroy
}

Why my form doesn't validate the fields?

I can't see a clear mistake in this code. Instead of validating my fields, it just tries to send my form and I don't know why.
This is my jsFiddle: http://jsfiddle.net/PAALA/
Other question, how to validate if select box was picked?
Firstly, because of how JSFiddle works, defining a function with function foo() {...} is unreliable. Instead, use foo = function() {...} syntax.
Next, you have an error in your script when you try to access document.forms["bug_form"]["Project"].value - there is no text input with that name.
Finally, to prevent accidental submission, do this:
validateBugForm = function() {
try {
// ALL YOUR ORIGINAL CODE HERE
}
catch(e) {
alert("An error occurred: "+e);
return false;
}
}
This will ensure that false is returned, even if your code errs.
The Javascript code is crashing out on the third line when you try to get the value for "Project". Looks like you forgot to give that one a name.

Categories