Home > Web Front-end > JS Tutorial > JS interesting eval optimization input validation example code_javascript skills

JS interesting eval optimization input validation example code_javascript skills

WBOY
Release: 2016-05-16 17:21:43
Original
1152 people have browsed it

Copy code The code is as follows:

//eval is to calculate the string [you can put any js The value in the code is
//1,
var str1='12 3';
eval(str1);//15

//2、
var str2='[1,2,3]';
eval(str2[0]);//1

//3.
eval('alert("abc")');//Pop up abc

//4.
var str="function show(){alert('love you');}";
eval(str);
show();

//5. eval--->json string

JS interesting eval optimization input validation example code_javascript skills

1. If the above user name and email verification input are blank, the traditional method is

Copy code The code is as follows:

var flag = true;
if (document. getElementById("txtName").value == "") {
                                                                                                                                                                  xtEmail").value = = "") {
                                                                                                                                                                                                ; 🎜>-->But how? There are many fields that need to be verified, there will be a lot of ifs, and they are not elegant at all

2. To calm down, let’s take a look at html





Copy code

The code is as follows:

Username:



Copy code

The code is as follows:

function validateForm() {


Copy code


The code is as follows:


function FormField(fieldName, fieldDesc) {//Encapsulate the changer attribute fields and descriptions
this.fieldName = fieldName;
this.fieldDesc = fieldDesc;
}
String.prototype.MyTrim = function () {//Remove leading and trailing spaces
                                                                                                                                                                                                     . >
var oUl = document.getElementById("ulError");
oUl.innerHTML = "";
var list = new Array
(
//You only need to verify that it is empty in the future Just add an object to the array
new FormField("name", "username"),
new FormField("email", "mail")
var flag = true; eval operation
if (fv == null ||!fv.MyTrim()) {
  fieldDesc " Cannot be empty liError.innerHTML =list[i].fieldDesc " Cannot be empty";
                                                                                                                                             🎜>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template