<FormId= "Registerform"Action=""Method= "POST"OnSubmit= "return Checkform ();">User name:<InputType= "Text"Id= "username"Name= "username"MaxLength= "20"Onblur= "Checkusername ()"/><SpanId= "Span1"></Span>Password:<InputType= "Password"Id= "Password"Name= "Password"MaxLength= "20"/> Confirm password: <input id= "Repassword" Type= "password" Name=" Repassword " Maxlength=" /> <input type=" submit " value = "register" ></form>
functionCheckform () {//Verify User name://Get the value of the User Name text box:var username = document.getElementById ("username"). Value;if (username = =null | | Username = = "'{alert ("User name cannot be empty!"));ReturnFalse; }//Check Password://Get the value of the Password box:var password = document.getElementById ("Password"). Value;if (password = =null | | Password = = "'{Alert ("Password cannot be empty!"));ReturnFalse; }//Check Confirm password:var Repassword = document.getElementById ("Repassword"). Value;if (Repassword! =Password) {alert ("Two times password input is inconsistent!"));ReturnFalse; }}functionCheckusername () {//Get File Box value:var username = document.getElementById ("username"). Value;//1. Creating an asynchronous interaction objectvar xhr =Createxmlhttp ();//2. Set the monitoring Xhr.onreadystatechange =function(){if (xhr.readystate = = 4){if (Xhr.status = = 200) {document.getElementById ("Span1"). InnerHTML =Xhr.responsetext; } } }//3. Open the Connection Xhr.open ("GET", "${pagecontext.request.contextpath}/user_findbyname.action?time=" +New Date (). GetTime () + "&username=" +username,True);//4. Send Xhr.send (Null);}functionCreatexmlhttp () {VarXmlHttp;try{// Firefox, Opera 8.0+, Safari xmlhttp=new XMLHttpRequest (); }catch (e) { Try{// Internet Explorer xmlhttp=new ActiveXObject ("msxml2.xmlhttp" catch (e) { Try{xmlhttp=new activexobject ("Microsoft.XMLHTTP" ); }catch (e) {}}} return< Span style= "COLOR: #000000" > xmlHttp;}
Public String Findbyname ()ThrowsIOException {//Call service to query: User existuser =Userservice.findbyusername (User.getusername ());//Get Response object, item page output: httpservletresponse response =Servletactioncontext.getresponse (); Response.setcontenttype ("Text/html;charset=utf-8"); determine if (existuser! = null) { // query to the user: the user name already exists response.getwriter (). println ("<font col or= ' Red ' > username already exists </font> "); } Else { // did not query to the user: User name can use Response.getwriter (). println ("<font color= ' green ' > username can be used </ Font> "); } return NONE;}
< Span style= "COLOR: #0000ff" > < Span style= "COLOR: #0000ff" > < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" >&NBSP;
Web front-end form validation