Asp.Net validation check at client side - asp.net

I am validating data at client side in asp.net validator by using following code snipet.
function ValidateData(){
if (!Page_ClientValidate("Validator1") || !Page_ClientValidate("Validator2")) {
return false;
}
else{
return true;
}
I called it on submit of button. But it showing validation messages of Validator1 group. Its not showing me validation messages of Validator2 group.

Just gone through :
see this link question , here its told - || operator short-circuits if the left condition is true.
Does a javascript if statement with multiple conditions test all of them?
If you want both , then cant you try like this :
function ValidateData(){
if (!Page_ClientValidate("Validator1"))
{
if (!Page_ClientValidate("Validator2"))
{
return false;
}
else
{
return false;
}
return false;
}
else
{
return true;
}
}
Just a random try , this code :)
Rigin

Related

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="";
}

spring and hibernate

I am beginner with spring, I have a search form and I want to error or the form is blank it sends me to the error page otherwise it sends me to the page result.
if anyone can help me
thank you
here is my code.
#SuppressWarnings( { "unchecked" })
#RequestMapping("/search.html")
public String findAllData(Map model, SearchForm seachForm) {
model.put("allData", searchService.searchData(seachForm));
if("seachForm"!=null)
{
return "displayForm";
}
else
{
return "redirect:/searchForm.htm";
}
if("seachForm"!=null){
return "displayForm";
}else{
return "redirect:/searchForm.htm";
}
seachForm is not a String, try with
if(seachForm != null){
return "displayForm";
}else{
return "redirect:/searchForm.htm";
}
Greetings.
As MG_Bautista showed searchForm is definitely not a String (you should have gotten a compile error), but searchForm would also never be null. It'll just be a bunch of zero-length Strings. You have to do iterate over all fields of searchForm like
if ("".equals(searchForm.getUserName()) &&
"".equals(searchForm.getAddress())) {
return "displayForm";
} else {
return "redirect:/searchForm.htm";
}

leave if statement

I have an if statement inside an if statement.
If the condition in the second if statement returns false, I want to go to the first else
because there it sets my validation controls automatically.
I hope you understand
if (page.isvalid() )
{
if (datetime.tryparse (date) == true)
{
// ok
}
else
{
//go to the other else
}
}
else
{
// want to go here
}
Edit:
Important is that I have to first validate the page because after the validation, I know I can parse the datetimes from the 2 input controls and check if the second one is greater than the first one. Otherwise it throws an exception, maybe, if the date is not valid.
instead of DateTime.Parse(date) use
DateTime dt;
bool isParsed = DateTime.TryParse(date, out dt);
//if ( page.isvalid() && (datetime.parse (date) == true) )
if ( page.isvalid() && isParsed )
{
// ok
}
else
{
// want to go here
}
Take out the elses, and it should be what you're looking for. Also, add a return statement after everything is good to go.
if ( page.isvalid() )
{
if (datetime.parse (date) == true)
{
// ok
}
return;
}
// code here happens when it's not valid.
This does exactly what you want, I believe.
if (page.isvalid() && datetime.tryparse(date) == true)
{
// ok
}
else
{
// want to go here
}
It is not clear whether the '== true' is necessary; you might be able to drop that condition.
Exactly what you want to do is impossible. There are two options. One is to determine both answers at the top of your if clause. This is what the other posters are telling you to do. Another option would be something like this:
bool isvalid = true;
if ( page.isvalid() )
{
if (datetime.tryparse (date) == true)
{
// ok
}
else
{
isvalid = false;
}
}
else
{
isvalid = false;
}
if (isvalid == false)
{
//do whatever error handling you want
}
This extracts the error handling code from your else clause and puts it somewhere both code paths can reach.

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;
}

Dropdownlist doesn't postback after Page_ClientValidate()

Update:
I have just found the solution. The following function works (remove the else part):
function confirmSubmit() {
if (Page_ClientValidate("Group1")) {
return window.confirm("Are you sure to submit the form?");
}
}
But I am wondering why it doesn't work when I add the else part.
Question:
I want to have a confirm dialog after user fills in all the data in the form.
I set onclientclick="return confirmSubmit()" in the submit button.
function confirmSubmit() {
if (Page_ClientValidate("Group1")) {
return window.confirm("Are you sure to submit the form?");
} else {
return false;
}
}
If Page_ClientValidate("Group1") returns false, the dropdownlist doesn't cause postback after I first select the item, and the postback only occurs when I select the dropdownlist second time.
What's the problem?
After Page_ClientValidate is called, the variable Page_BlockSubmit gets set to true, which blocks the autopost back. Page_BlockSubmit was getting reset to false on the second click, for what reasons I still don't fully understand. I'm looking more into this, but I have a solution and I'm under the gun so I'm rolling with it....
Just add below code in the code block which executes if Page is not valid.
Page_BlockSubmit = false;
e.g.
function ValidatePage()
{
flag = true;
if (typeof (Page_ClientValidate) == 'function')
{
Page_ClientValidate();
}
if (!Page_IsValid)
{
alert('All the * marked fields are mandatory.');
flag = false;
Page_BlockSubmit = false;
}
else
{
flag = confirm('Are you sure you have filled the form completely? Click OK to confirm or CANCEL to edit this form.');
}
return flag;
}
I have just found the solution. The following function works (remove the else part):
function confirmSubmit() {
if (Page_ClientValidate("Group1")) {
return window.confirm("Are you sure to submit the form?");
}
}

Resources