Javascript Form Verification and button jump

Source: Internet
Author: User

We designed a form for verification on the local client. If the verification is not correct, submission is not allowed. The jump is allowed only when the verification is complete and correct.

1. JavaScript code:

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">

<Title> JavaScript prohibiting form submission </title>

<SCRIPT type = "text/JavaScript">

Function getobj (ID ){

VaR OBJ = Document. getelementbyid (ID). value;

Return OBJ;

}

 

Function check (){

If (getobj ("test") = ""){

Alert ("the input in the text box is empty. The form cannot be submitted! ");

Document. getelementbyid ("test"). focus;

Return false; // false: prevents form submission.

}

}

</SCRIPT>

</Head>

<Body>

<Form action = "index.htm" method = "Post" onsubmit = "Return check ()">

<Input type = "text" name = "test" id = "test">

<Input type = "Submit" value = "Submit">

</Form>

</Body>

</Html>

2. jquery code

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">

<SCRIPT type = "text/JavaScript" src = "/sunchis/script/jquery-1.3.2.js"> </SCRIPT>

<SCRIPT type = "text/JavaScript">

$ (Document). Ready (function (){

$ (": Submit [ID = tijiao]"). Click (function (check ){

Var val = $ (": Text [ID = test]"). Val ();

If (val = ""){

Alert ("the input in the text box is empty. The form cannot be submitted! ");

$ (": Text [ID = test]"). Focus ();

Check. preventdefault (); // This prevents form submission.

}

});

});

</SCRIPT>

<Title> jquery prohibits form submission. </title>

</Head>

<Body>

<Form action = "index.htm" method = "Post">

<Input type = "text" name = "test" id = "test">

<Input type = "Submit" id = "tijiao" value = "Submit">

</Form>

</Body>

</Html>

3. hyper-connection jump

During hyper-connection, we often connect to a JSP or action, for example:
Xxx
In this case, there will be a problem: in test. in JSP, except the param1 and param2 parameters you pass, you use request. if getparamter () is used to retrieve any form element of the original page, it will be null, because this method will generate a new request lifecycle. In this request, it will only contain the elements that follow after the superjoin? Param1, so I suggest that you do not use this hyper-connection method if it is not a simple page Jump (for example, a logon page.

4. Passing parameters and submitting forms

If you want to pass parameters in the first way and submit a form, you can do the following:
XXX is processed as follows in the script:
Function commonsubmit (URL)
{
Form1.action = "/JSP/manageraction. do? "+ URL;
Form1.submit ();
}
Commit to action is the same as JSP, but remember
Method = post cannot be less; otherwise, it only submits the form without passing the param parameter (opposite to the first one :))

5. Mutual transmission of JS and Java Variables

In JSP, JS variables are often assigned to Java variables, or Java variables are assigned to JS variables. Here we will summarize the general processing methods as follows:
It is easy to pass Java variables to JS. var A = ""; note that you should add the quotation marks "". js variables are slightly more complex for Java. Generally,
And then assign the JS variable value to it in the script:
VaR jsparamvalue = AAAA;
Form1.jsparam. value = jsparamvalue;
Then you can request. getprameter ("jsprama"); To Get The JS variable value.

6. the request in the action will not be lost.

I wonder whether you have noticed this benefit. In the struts architecture, an actionservlet acts as the controller of MVC. After submitting a JSP page, the request is uploaded to the actionservelt, the actionservlet will call the corresponding action method according to the configuration in the struts-config.xml, and pass the request obtained from the JSP to the action class, so that the request lifecycle is continuous, that is, you submitted the form in JSP, executed the method in action, and then returned to the JSP page, using request. getparamter () takes the form Element value of the JSP page and finds that it is still there. This is a good implementation method for selecting items such as the page drop-down list to prevent resetting.

7. When passing parameters, there is a space.

If you submit a form with parameters such as form1.action = "/JSP/action. do? Param1 = "+ value1. Note that if value1 contains spaces, you must request in the action. getparameter ("param1"); obtains only the value before the space. Therefore, if a parameter with a space is passed, it is recommended that it be converted to a specific string, value1 = value1.replaceall ("", "% null %"); then return it in action: request. getparamter ("param1 "). replaceall ("% null % ","");

8. Form-data attributes

If you want to upload files, note that if the enctype = "multipart/form-Data" attribute exists in a form, is not allowed to receive other form element types except type = file. That is, if you put and put in the same form, and the form has enctype = "multipart/form-Data", request. the value of getparamter text is null. The simplest solution to this problem is to put a form separately, and only submit the form when uploading the file.

9. Open Mode dialog box in JSP

Similar to swing, JSP also has the pattern dialog box concept. You can place a JSP page in a pattern dialog box to open it, so that before the pattern dialog box disappears, the original JSP page cannot be operated. It is especially suitable for the parent page to do some additional selection operations, but there is no need to jump to the new JSP page,
The Mode dialog box is as follows:
Function open (){
If (window. showmodeldialog ())
{
VaR returnvalue = showmodeldialog ("/JSP page path included in the mode window ";
Help = 0; status = 0; center = yes; dialogwidth = 100pt; dialogheight = 100pt ");
}
}

Related Article

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.