自己写的前端验证--验证validation.js文件(附代码)
程序员文章站
2022-04-16 20:24:52
...
下面是验证的validation.js文件 var errMsg = [ ' String length must be greater than 4 characters', '名字必须汉字', '年龄必须为数字', '密码必须多于或等于 6 个字符。', '验证密码与原密码不一致!', 'Email地址不合法!', ]; var pattern = [ /.{4,}/, /^([\u4E00-\u9FA5]){1,}$/, /^[0-9]{1,3}$/, /.{6,}/, '', /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]{2,}(\.[a-z0-9]{2,5}){1,2}$/ ]; function CheckLengthNG(TagValue,TagName,Message) { if (!pattern[0].test($(TagValue).val())) { $(TagName).html(Message + errMsg[0]); return true; } else { $(Display).html(''); return false; } } } function showErrorMessage(TagName, ErrorMessage) { var a = ErrorMessage.substring(ErrorMessage.indexOf('['), ErrorMessage.indexOf(']')+1) var b = '<span style="color:skyblue">' + a + '</span>' var ErrMsg = ErrorMessage.replace(a, b); $(TagName).html(ErrMsg); } 下面是使用的页面 @{ ViewBag.Title = "Document SIC"; //Layout = null; } <script src="~/Scripts/jquery-1.10.2.js"></script> <script src="~/Scripts/common.js"></script> <script type="text/javascript"> function InsertDocumentSIC() { var SICNo = $('#txtSICNo').val(); var TemplateID = $('#selDocumentTemplate').val(); if (CheckLengthNG('#txtSICNo', '#SICspanErrorMessage', 'The SIC No ')) { return; } var obj = GetTemplateID(); if (obj.statusCode != 0) { return; } else { var TemplateID = obj.TemplateID } var url = "/Document/InsertDocumentSIC?TemplateID=" + TemplateID + "&SICNo=" + SICNo; $.ajax({ url: url, async: false, type: "POST", contentType: "application/json", success: function (data) { var objData = JSON.parse(data) if (objData.statusCode != '0') { alert(objData.statusMessage) showErrorMessage('#SICspanErrorMessage', objData.statusMessage); $('#txtSICNo').select(); }else { GetDocumentSIC(); $('#txtSICNo').val(''); } } }); } </script> <!--Attachment 2--> <p id="pDocumentSICUserAuthority" > <p class="row"> <label style="color:white">SIC No </label> <input type="text" id="txtSICNo" /> <button class="btn btn-primary" onclick="InsertDocumentSIC()" id="btnDocumentAddSIC">Add SIC</button> <span id="SICspanErrorMessage" style="color:red"> </span> </p> <p class="row table-responsive"> <table id="TableSIC" class="table table-bordered text-nowrap" style="color:white;background-color:transparent;"> <thead> <tr> <th>ID </th> <th>SIC Title </th> <th>SIC No </th> <th>Revision </th> <th>Effecitve Date </th> <th>Digital Signature </th> <th>Date </th> <th>updQty </th> <th>Delete </th> <th>Upload </th> </tr> </thead> <tbody></tbody> </table> </p> </p>
相关文章:
相关视频:
以上就是自己写的前端验证--验证validation.js文件(附代码)的详细内容,更多请关注其它相关文章!