C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » JavaScript »

java script validation


Posted Date: 29 Nov 2008      Posted By: sarayu      Member Level: Gold     Points: 1   Responses: 3



Hi all,

Here i am using java script code to validate first name,date,mobile number and email. i write below line to show alert message

alert('Please Enter Required Fields\n\n* Indicates Required fields');

my query is when showing this message, alert box have "ok" button, when i click ok button i want to focus which fields are left blank to the end user.

i wrote below line to focus

document.getElementById('txtDate').focus(); but i think this line is not working.

below is my entire code. Please help me.

function Validation()
{

var Alpha = "^[a-zA-Z]+$";
var Numeric = "^[0-9]+$";
var Date = "^[0-9/]+$";
var address = "^[a-zA-Z0-9#]+$";
var Email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/;


var txtFirstName=document.getElementById('TxtFirstname').value;
var txtDate=document.getElementById('TxtDate').value;
var txtMobileNumber = document.getElementById('TxtMobileno').value;
var txtEmail = document.getElementById('TxtEmailid').value;


//validation for Date
if(txtDate == "")
{
alert('Please Enter Required Fields\n\n* Indicates Required fields');
return false;
}
else
{
if(txtDate.search(Date) == -1)
{
alert('Please Select Date from calendar');
return false;
document.getElementById('txtDate').focus();
}
}


//validation for first name
if(txtFirstName == "")
{
alert('Please Enter Required fields\n\n* Indicates Required fields')
}
else
{
if(txtFirstName.search(Alpha) == -1)
{
alert('Name must be Characters only');
return false;
document.getElementById('txtFirstName').focus();
}
}


//Validation for Mobile Number
if(txtMobileNumber == "")
{
alert('Please Enter Required fields\n\n* Indicates Required fields')
return false;
}
else if(txtMobileNumber.length < 12)
{
alert('Mobile Number must be 12 digits\n\n Enter with country code 91');
return false;
}

else
{
if(txtMobileNumber.search(Numeric) == -1)
{
alert('Mobile Number must be Numeric');
return false;
document.getElementById('txtMobileNumber').focus();
}
}


//Validation for Email
if(txtEmail != "")
{
if(txtEmail.search(Email) == -1)
{
alert('Please enter a valid Email ID');
return false;
document.getElementById('txtEmail').focus();
}
}


return true;
}

thank you...





Responses

Author: narenderraobalguri    29 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 4

Hi, Try like this .Place focus statement before Return statement

if(txtDate == "")
{
alert('Please Enter Required Fields\n\n* Indicates Required fields');
return false;
}
else
{
if(txtDate.search(Date) == -1)
{
alert('Please Select Date from calendar');

document.getElementById('txtDate').focus();
return false;
}

Regards,
Narender



Author: Arun Jacob    29 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 3

Give like this,


if(txtMobileNumber.search(Numeric) == -1)
{
alert('Mobile Number must be Numeric');
document.getElementById('txtMobileNumber').focus();
return false;
}


Arun Jacob
http://codepronet.blogspot.com/



Author: Muhammad Faseeh Abrar    01 Dec 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 3

The Problem with your code is this that you focus the control after the return statement.
Your problem will be solved if you focus the control first and then execute the return statement.

Regards
Muhammad Faseeh Abrar



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : outlook Attachment using java script mailto function???
Previous : How to use javascript validation in serverside controls?
Return to Discussion Forum
Post New Message
Category: JavaScript

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use