jquery Struts Verify unique identification (public method) _jquery

Source: Internet
Author: User
Tags unique id
Step one introduce JS file
Copy Code code as follows:

<script language= "javascript1.2" src= "<s:url value="/js/jquery.js "includeparams=" false "/>" ></ Script>
<script language= "javascript1.2" src= "<s:url value="/js/dictionary.js "includeparams=" false "/>" ></ Script>

Step two public js file Dictionary.js
Copy Code code as follows:

/**
*↓↓↓↓↓↓↓↓↓↓
* Author: Zhuangzi
* Class Name: Public method initialization
* Function: Public method initialization
* Detailed: Common method initialization
* Version: 1.0
* Date: 2013-03-22
Description
*↑↑↑↑↑↑↑↑↑↑
*/
$ (function () {
/*1. Loss of focus verification ↓↓↓*/
$ ("#code"). blur (callback);//1.code is the control ID 2 for the unique flag you want to validate. You must have a control behind your control that receives the error message
/*2. Validate ↓↓↓*/at time of submission
$ ("#submit"). Click (callback)//The Submit button ID of your page
});
/**
*↓↓↓↓↓↓↓↓↓↓
* Author: Zhuangzi
* Class Name: Verifying unique identification Common method
* Function: Unique validation
* Details: Unique ID and submit button ID to be consistent with the following
* Version: 1.0
* Date: 2013-03-22
Description
*↑↑↑↑↑↑↑↑↑↑
*/
function callback () {
var slef=$ ("#code");
var code=slef.val ();
var submit=$ ("#submit");
var flag=$ ("#flag"). Val ();
if (null==code| | "" ==code) {
Slef.next (). html ("Download package ID cannot be empty!");
Submit.attr ("Disabled", true);
Return
}else{
if (!code.match ("^\\w+$")) {
Slef.next (). HTML ("The identity consists of several letters or underscores!");
return;
}
Slef.next (). HTML ("*");
}
$.ajax ({
Type: ' Post ',
URL: '.. /dictionary/checkcodeonly.do ',
Data: ' bean.code= ' +code+ ' &bean.flag= ' +flag,
DataType: ' JSON ',
Success:function (JSON) {
if (json>0) {
Slef.next (). HTML ("This identity already exists!");
Submit.attr ("Disabled", true);
return;
}else{
Slef.next (). HTML ("");
if (Code!= null && code!= "") {
Submit.attr ("Disabled", false);
}else{
Submit.attr ("Disabled", true);
Return
}
}
},
Error:function () {
Alert (' Verify information error ');
}
});
}

Step three page section
Copy Code code as follows:

<s:hidden id= "Flag" value= "3"/> <!-flag what type of flag->
<tr>
&LT;TD width= "40%" height= "class=" "ADDTABLETD1" > Download logo:</td>
&LT;TD width= "60%" height= "class=" Addtabletd2 ><div "left" >
<s:textfield id= "code" name= "Yhaoportalsdownbean.downflag" cssclass= "Textfrom" cssstyle= "width:150px"; Maxlength= "/>"
<font id= "Codeinfo" color= "Red" >*</font>
</div></td>
</tr>

step four method in action
Copy Code code as follows:

/**
*
* @author Zhuangzi
* @class com.hzdracom.action.YhaoPortalsDictionaryAction
* @method checkcodeonly
* @Directions Validation Unique identification common method
* @date 2013-3-21 a.m. 10:09:04 void
*/
public void Checkcodeonly () {
String json= "";
try{
JSON = string.valueof (yhaodictionaryservice.checkcodeonly (bean));
Json=json.tojsonstring (JSON);
System.out.println ("json==" +json);
HttpServletResponse Response=servletactioncontext.getresponse ();
Response.setcontenttype ("text/html");
Response.setcharacterencoding ("Utf-8");
PrintWriter out;
out = Response.getwriter ();
OUT.PRINTLN (JSON);
Out.flush ();
Out.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}

step five method in DAO
Copy Code code as follows:

<pre Class=java name= "code" > public int checkcodeonly (Dictionarybean bean) throws Dataaccessexception,exception {
object[] Sqlparams = new Object[4];
int index = 0;
String sql= "";
/* Verify that the page flag is unique * *
if (Bean.getflag (). Equals ("1")) {
sql = "SELECT COUNT (1) from yhao_portals_page where Page_flag =?";
Sqlparams[index]=bean.getcode ();
index++;
}
/* Verify that the type of log ID is unique * *
if (Bean.getflag (). Equals ("2")) {
sql = "SELECT COUNT (1) from yhao_portals_downtype where Downtype_flag =?";
Sqlparams[index]=bean.getcode ();
index++;
}
/* Verify that the download log ID is unique * *
if (Bean.getflag (). Equals ("3")) {
sql = "SELECT COUNT (1) from yhao_portals_down where Down_flag =?";
Sqlparams[index]=bean.getcode ();
index++;
}
if (Bean.getflag (). Equals ("4")) {
sql = "SELECT COUNT (1) from Yhao_portals_kuai where Kuai_flag =?";
Sqlparams[index]=bean.getcode ();
index++;
}
object[] Sqlparamsend = new Object[index];
System.arraycopy (sqlparams, 0, sqlparamsend, 0, index);
int count = This.queryforint (sql,sqlparamsend);
return count;
}</pre>
<PRE></PRE>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.