Procházet zdrojové kódy

added auth validation

Alex před 5 roky
rodič
revize
7914a15d7b

+ 12 - 0
src/common/authRenderField.js

@@ -0,0 +1,12 @@
+import React from "react";
+
+export const authRenderField = ({input, meta: {touched, error}, label, type, placeholder }) => (
+    <div className={touched && error ? "form__input-box":"error"}> 
+        {console.log(input.value)} 
+        <label className="form__input-label" htmlFor="origin">
+            {label}
+            <input  className="form__input" type = {type} placeholder = {placeholder} {...input} />
+        </label>
+        {touched && error && <span>{error}</span>}
+    </div>
+);

+ 12 - 0
src/common/regRenderField.js

@@ -0,0 +1,12 @@
+import React from "react";
+
+export const regRenderField = ({input, meta: {touched, error}, label, type, placeholder }) => (
+    <div className="form__input-box">
+    {console.log(input.value)}
+        <label className="form__input-label" htmlFor="origin">
+            {label}
+            <input  className="form__input" type = {type} placeholder = {placeholder} {...input} />
+        </label>
+        {touched && error && <span>{error}</span>}
+    </div>
+);

+ 19 - 0
src/utils/authValidate.js

@@ -0,0 +1,19 @@
+export const authValidate =  values => {
+    const { email, password } = values;
+    const error = {};
+
+
+    if(!email) {
+        error.email = "Required"
+    } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.]+\.[A-Z]{2,4}$/i.test(email)) {
+        error.email = 'Invalid email address'
+    }
+
+    if(!password) {
+        error.password = "Required"
+    }else if(!/^[a-zA-Z0-9]+$/.test(password)) {
+        error.password = "Invalid password"
+    }
+
+    return error;
+};

+ 38 - 0
src/utils/regValidate.js

@@ -0,0 +1,38 @@
+export const regValidate = values => {
+    const { login, password, name, phone, email } = values;
+    const error = {};
+
+    if (!login) {
+        error.login = "Required" // - true если ничего не надо возвращать
+    } else if (login.length > 15) {
+        error.login = 'Must be 15 characters or less'
+    } else if (!/^[a-zA-Z0-9]+$/.test(login)) {
+        error.login = "invalid login"
+    }
+
+    if (!password) {
+        error.password = "Required"
+    }else if(!/^[a-zA-Z0-9]+$/.test(password)) {
+        error.password = "Invalid password"
+    }
+
+    if (!name) {
+        error.name = "Required"
+    }else if (!/^[a-zA-Z0-9]+$/.test(name)) {
+        error.name = "Invalid login"
+    }
+
+    if (!phone) {
+        error.phone = "Required"
+    }else if(!/^[0-9-+()]+$/.test(phone)) {
+        error.phone = "Invalid phone number"
+    }
+
+    if (!email) {
+        error.email = "Required"
+    } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.]+\.[A-Z]{2,4}$/i.test(email)) {
+        error.email = 'Invalid email address'
+    }
+
+    return error;
+};