Home > Software design >  jQuery email validation from mysql
jQuery email validation from mysql

Time:12-29

I have two codes. the one code, I have the data entered in the on the user registration page checked (all textboxes must be filled, password length,is the email a real email, etc...). the other code, it sends the email entered in the form to MySQL and answers whether it has been registered before. it works fine when run separately,but when i try to combine two of them codes,i cant run it at all. ///EMAIL CHECK FROM DATABASE/// the code i wrote, ////FORM VALIDATION//// i want to use it with the code i wrote with the title. As you can understand, the e-mail control starting the from the 16th line first checks whether the input is empty. Then it checks whether the value entered in the input is in a real e-mail format and says setSuccess() and finishes the process but as a third verification, i want to check whether the e-mail is in the database but as i just mentioned, when i combine the two codes, I can't run it.

////FORM VALIDATION////
1 const isValidEmail = email => {
2   const re = /^(([^<>()[\]\\.,;:\s@"] (\.[^<>()[\]\\.,;:\s@"] )*)|(". "))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9] \.) [a-zA-Z]{2,}))$/;
3    return re.test(String(email).toLowerCase());
4 }
5
6 const validateInputs = () => {
7   const usernameValue = username.value.trim();
8   const emailValue = email.value.trim();
9    const passwordValue = password.value.trim();
10
11    if(usernameValue === '') {
12        setError(username, 'Username is required');
13    } else {
14        setSuccess(username);
15    }

16  if(emailValue === '') {
17        setError(email, 'Email is required');
18    } else if (!isValidEmail(emailValue)) {
19        setError(email, 'Provide a valid email address');
20    } else {
21        setSuccess(email);
22        
23    }
24
25    if(passwordValue === '') {
26        setError(password, 'Password is required');
27    } else if (passwordValue.length < 8 ) {
28        setError(password, 'Password must be at least 8 character.')
29    } else {
30        setSuccess(password); 
31    }

    
    

32 };
////FORM VALIDATION////
///EMAIL CHECK FROM DATABASE///
$(document).ready(function(){
        $('#email').blur(function(){
            var email = $(this).val();
            $.ajax({
                
                url:'test.php',
                method:"POST", 
                data:{e_mail:email},
                success:function(data){
                   if(data != '0')
                        {
                            $('#error_message').html("Sorry, the email already has been taken.");
                           

                            return; 
                            
                        } 
                    else{
                        $('#error_message').html("");
                  
                        

                        return;
                    }
                    }
                
            })
        });
        
    });
///EMAIL CHECK FROM DATABASE///

CodePudding user response:

To solve your problem you just need to re use your previous code : so replace line 21 with this :

        $.ajax({
            url:'test.php',
            method:"POST", 
            data:{e_mail:emailValue },
            success:function(data){
               if(data != '0')
                    {
                     setError(email, 'Sorry, the email already has been taken.');
                        
                    } 
                else{
                    setSuccess(email);
                }
              }
            
        });

But like I commented, in Web development you should NEVER relay on the client side FORM VALIDATION. It's so useful to lower the amount of postbacks to your server ( because you give your server prepared data to control only once ) , but never enough to make a new registration/login .

  • Related