I have two Javascript functions that I have used - the first one used to work, the second one has issues too
<script type="text/javascript">
function WebForm_OnSubmit()
{
if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
{
var prevcontrol = "a";
for (var i in Page_Validators)
{
try
{
var control = document.getElementById(Page_Validators[i].controltovalidate);
if (Page_Validators[i].isvalid)
{
control.className = "has-success";
}
if (!Page_Validators[i].isvalid)
{
control.className = "has-warning";
prevcontrol = control;
}
if (control = prevcontrol)
{
control.className = "has-warning";
}
} catch (e) { }
}
return false;
}
return true;
}
</script>
This one doesn't seem to work at all!
<script type="text/javascript">
function BtnClick()
{
var val = Page_ClientValidate();
if (!val)
{
var i = 0;
for (; i < Page_Validators.length; i++)
{
if (!Page_Validators[i].isvalid)
{
$("#" + Page_Validators[i].controltovalidate)
.css("border-color", "red");
}
if (Page_Validators[i].isvalid)
{
$("#" + Page_Validators[i].controltovalidate)
.css("border-color", "#5cb85c");
}
}
}
return val;
}
</script>
This one returns the first control as invalid but marks all others valid even if they are not.
I created the first one because some of the textboxes either have requiredfield or regex, and it detects if either one is invalid.
<script type="text/javascript">
function WebForm_OnSubmit() {
if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) {
var prevcontrol = "a";
for (var i in Page_Validators) {
try {
var control = document.getElementById(Page_Validators[i].controltovalidate);
if (Page_Validators[i].isvalid) {
$(control).addClass("form-control-valid");
}
if (!Page_Validators[i].isvalid) {
$(control).addClass("form-control-invalid");
prevcontrol = control;
}
if (control = prevcontrol) {
$(control).addClass("form-control-invalid");
}
} catch (e) { }
}
return false;
}
return true;
}
</script>
This is closer, I get validation, but I am still getting a valid when invalid if the control has 2 validators attached to it. So if I have a required field and if I have a regex, if the reqfld is invalid because there is no input, it returns valid because
the regex is not registering invalid
<script type="text/javascript">
function WebForm_OnSubmit()
{
if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
{
var prevcontrol = "a";
var valid = 1;
for (var i in Page_Validators)
{
try
{
var control = document.getElementById(Page_Validators[i].controltovalidate);
if (control != prevcontrol )// If New Control is not the old control
{
if (Page_Validators[i].isvalid) // If Item is Valid
{
$(control).addClass("form-control-valid"); // Make Green
valid = 1; //Set to Valid
}
if (!Page_Validators[i].isvalid) // If Item is Not Valid
{
$(control).addClass("form-control-invalid"); // Make Red
valid = 0; //Set to Invalid
}
}
if (control == prevcontrol) //If New control is the same as previous control, then second validator is active
{
$(control).removeClass("form-control-valid"); //remove CssClasses
$(control).removeClass("form-control-invalid"); //remove CssClasses
if (valid == 1) //If First Validator was valid
{
if (Page_Validators[i].isvalid) // If Second validator and First Validator are valid
{
$(control).addClass("form-control-valid"); //Make Green
}
if (!Page_Validators[i].isvalid) // If Second validator is invalid but First Validator is valid
{
$(control).addClass("form-control-invalid"); //Make Red
}
}
if (valid == 0) //If First Validator was invalid
{
$(control).addClass("form-control-invalid"); // Make Red because it doesnt matter about the second if the first was invalid!
}
}
prevcontrol = control;
}
catch (e) { }
}
return false;
}
return true;
}
</script>
This code works with single validators or double validators - place at end of page, and add to form
<script type="text/javascript">
function WebForm_OnSubmit()
{
if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
{
var prevcontrol = "a";
var valid = 1;
for (var i in Page_Validators)
{
try
{
var control = document.getElementById(Page_Validators[i].controltovalidate);
if (control != prevcontrol )// If New Control is not the old control
{
if (Page_Validators[i].isvalid) // If Item is Valid
{
$(control).addClass("form-control-valid"); // Make Green
valid = 1; //Set to Valid
}
if (!Page_Validators[i].isvalid) // If Item is Not Valid
{
$(control).addClass("form-control-invalid"); // Make Red
valid = 0; //Set to Invalid
}
}
if (control == prevcontrol) //If New control is the same as previous control, then second validator is active
{
$(control).removeClass("form-control-valid"); //remove CssClasses
$(control).removeClass("form-control-invalid"); //remove CssClasses
if (valid == 1) //If First Validator was valid
{
if (Page_Validators[i].isvalid) // If Second validator and First Validator are valid
{
$(control).addClass("form-control-valid"); //Make Green
}
if (!Page_Validators[i].isvalid) // If Second validator is invalid but First Validator is valid
{
$(control).addClass("form-control-invalid"); //Make Red
}
}
if (valid == 0) //If First Validator was invalid
{
$(control).addClass("form-control-invalid"); // Make Red because it doesnt matter about the second if the first was invalid!
}
}
prevcontrol = control;
}
catch (e) { }
}
return false;
}
return true;
}
</script>
This code works with single validators or double validators - place at end of page, and add to form
Do you mean you have solved the problem by yourself, and above code is the solution? If that is the case, thanks for sharing your solution, it might help
others to resolve similar threads.
Best regards,
Dillion
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
8 Points
100 Posts
Javascript Form Validation
Jan 14, 2017 05:53 PM|RobertH3|LINK
I have two Javascript functions that I have used - the first one used to work, the second one has issues too
This one doesn't seem to work at all!
This one returns the first control as invalid but marks all others valid even if they are not.
I created the first one because some of the textboxes either have requiredfield or regex, and it detects if either one is invalid.
Member
8 Points
100 Posts
Re: Javascript Form Validation
Jan 14, 2017 06:51 PM|RobertH3|LINK
This is closer, I get validation, but I am still getting a valid when invalid if the control has 2 validators attached to it. So if I have a required field and if I have a regex, if the reqfld is invalid because there is no input, it returns valid because the regex is not registering invalid
Member
8 Points
100 Posts
Re: Javascript Form Validation
Jan 14, 2017 10:56 PM|RobertH3|LINK
This code works with single validators or double validators - place at end of page, and add to form
All-Star
45489 Points
7008 Posts
Microsoft
Re: Javascript Form Validation
Jan 16, 2017 06:44 AM|Zhi Lv - MSFT|LINK
Hi RobertH3,
Do you mean you have solved the problem by yourself, and above code is the solution? If that is the case, thanks for sharing your solution, it might help others to resolve similar threads.
Best regards,
Dillion
Member
20 Points
31 Posts
Re: Javascript Form Validation
Jan 25, 2017 11:02 AM|ifour.nishith@gmail.com|LINK
Hi,
You use the below function and working with Javascript Form Validation
Software developer
custom software development company India