SharePoint Validate list form fields.

Sometimes we need to validate the input provided by the users in SharePoint list form fields. This can be easily achieved using Jquery in SharePoint. Suppose we want to ensure that the value provided by the user in Title field in NewForm.aspx does not start with digit 1. Following is the script to do exactly that


<script type="text/javascript" src="/Shared%20Documents/JS/jquery.min.js"></script>
<script language="javascript">
    $(function(){
        var titleField = $("input[title=Title]");
        titleField.parent().append("<div id='Error Message'> </div>");
        titleField.keyup(function(){
        ValidateTitleField(titleField.val());
        });

        function ValidateTitleField(value)
        {
            if (value.match(/^1/))
            {
                $("div[id=Error Message]").text("Title cannot start with 1");
                $("div[id=Error Message]").attr("style","color:red");
                EnableDisableOKButton(true);
            }
            else
            {
                $("div[id=Error Message]").text("");
                EnableDisableOKButton("");
            }
        }

        function EnableDisableOKButton(value)
        {
            $(".ms-ButtonHeightWidth").each(function()
            {
                var inputValue = $(this).attr('value');
                if (inputValue == "OK")
                {
                    $(this).attr("disabled",value);
                }
            });
        }
    });
</script>


Copy and paste the above script in the content editor webpart in the NewFrom.aspx. NewPage.aspx can be edited by appending 
?PageView=Shared&ToolPaneView=2 in the url.Your url shoulld look something like this http://ws2003/Lists/SPFF%20Tasks/NewForm.aspxPageView=Shared&ToolPaneView=2
The screen shot shows the validation performed on a Title field.


profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+