How to Use java script validation on SharePoint 2010 List.
Hi frnds,, We can make java script validation on sharepoint 2010 list. like--- Required Field,Comparition Field and Email validation.
Fo rDoing This Task you have to Follow some step..
1) make visual web part..
paste below code in .ascx page.
<table class="style1">
<tr>
<td>
<asp:Label
ID="Label1" runat="server" Text="Name:-"></asp:Label>
</td>
<td>
<asp:TextBox
ID="txtName" runat="server"/>
</td>
</tr>
<tr>
<td>
<asp:Label
ID="Label2" runat="server" Text="Email :-"></asp:Label>
</td>
<td>
<asp:TextBox
ID="txtEmail" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label
ID="Label3" runat="server" Text="Password :-"></asp:Label>
</td>
<td>
<asp:TextBox
ID="txtpwd" runat="server" TextMode="Password"
/>
</td>
</tr>
<tr>
<td>
<asp:Label
ID="Label4" runat="server" Text="Comfirm Password :-"></asp:Label>
</td>
<td>
<asp:TextBox
ID="txtcpwd" runat="server" TextMode="Password"
/>
</td>
</tr>
<tr>
<td>
<asp:Button
ID="btnSubmit" OnClientClick=" return validate()" runat="server"
Text="Submit"
onclick="btnSubmit_Click"
/>
</td>
<td>
</td>
</tr>
</table>
<script
language="javascript" type="text/javascript">
function
validate()
{
if (document.getElementById("<%=txtName.ClientID %>").value ==
"")
{
alert("Name
Feild can not be blank");
document.getElementById("<%=txtName.ClientID
%>").focus();
return
false;
}
if
(document.getElementById("<%=txtEmail.ClientID %>").value ==
"")
{
alert("Email
id can not be blank");
document.getElementById("<%=txtEmail.ClientID
%>").focus();
return
false;
}
var
emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var
emailid = document.getElementById("<%=txtEmail.ClientID %>").value;
var
matchArray = emailid.match(emailPat);
if
(matchArray == null)
{
alert("Your
email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID
%>").focus();
return
false;
}
if
(document.getElementById("<%=txtpwd.ClientID %>").value == "")
{
alert("Password
Feild can not be blank");
document.getElementById("<%=txtpwd.ClientID
%>").focus();
return
false;
}
if
(document.getElementById("<%=txtcpwd.ClientID %>").value == "")
{
alert("Comfirm
Password Feild can not be blank");
document.getElementById("<%=txtcpwd.ClientID
%>").focus();
return
false;
}
var
pwdpat = document.getElementById("<%=txtpwd.ClientID %>").value;
var
cpwdpat = document.getElementById("<%=txtcpwd.ClientID %>").value;
var
matchpwd = cpwdpat.match(pwdpat)
if
(matchpwd == null)
{
alert("Password
Does Not Match");
document.getElementById("<%=txtcpwd.ClientID
%>").focus();
return
false;
}
}
function
CloseSharePointDialog(url, message) {
alert(message);
returns
= false;
// window.location.href
= url;
window.frameElement.commitPopup();
}
</script>
2) Paste Below code in .ascx.cs page.
protected void btnSubmit_Click(object sender, EventArgs e)
{
SPListItem
item = null;
SPWeb
web = SPContext.Current.Web;
//try
//{
//***
Get Structure of Employee List
SPList
list = WSSHelper.GetList(testing);
item
= list.Items.Add();
//***
set field values
item["Title"]
= txtName.Text; // Title
item["email"]
= txtEmail.Text; // First Name
item["password"]
= txtcpwd.Text; // Password
web.AllowUnsafeUpdates
= true;
item.Update();
web.AllowUnsafeUpdates
= false;
string
strRedirect = SPContext.Current.Web.Url;
string
strMessage = "Item has been Successfully Added";
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"CloseDialog", "CloseSharePointDialog('" + strRedirect +
"','" + strMessage + "');", true);
}
3) Now build and deploy your web part..
4)add your web part on this
link..
http://YOUR SITE URl/Lists/YOUR LIST NAME/AllItems.aspx
5) Refresh the page..
and add new item your validation will work
successfully..
Thank's and Regards.
~~Radhe
| Pradip Panchal author of How to Use java script validation on SharePoint 2010 List. is from Ahmedabad, India. Pradip Panchal says Hello Everyone, I am Pradip Panchal from India | |
| | View All Articles |
|
Please enter your Comment
- Comment should be atleast 30 Characters.
- Please put code inside [Code] your code [/Code].
|
|
|
|
|
|
|