Register user validation script

mail@pastecode.io avatar
unknown
javascript
a year ago
1.9 kB
1
Indexable
Never
// ---- var declaration ---- //
var email = document.getElementById('email')
var password = document.getElementById('password')
var form = document.getElementById('form')
var ErrorElement = document.getElementById('error')
var password2 = document.getElementById('password2')
var gender = document.querySelector('#gender')
var city = document.getElementById('city')
var code = document.getElementById("code")
var phone = document.getElementById('phone')
var Fname = document.getElementById('Fname')
var Lname = document.getElementById('Lname')

let messages = []

// --- functions
function validateLogin()
  {
    console.log("in register validate")
    validateInput()
  if(messages.length>0){
    ErrorElement.innerHTML= messages.join(', ')
    messages = []
    return false;
  }
    messages = []
    return true;
  }

function validateInput()
  {
  if(password.value.length < 8){
    messages.push('Please enter a longer password')
  }
  if(email.value.includes('@')==false){
    messages.push('Please enter a correct email')
  }
  if(phone.value.length < 10){
    messages.push('Please enter a valid phone number')
  }
  if(password2.value != password.value){
    messages.push('The passwords dont match')
  }
  if(Fname.value.length < 2 || Lname.value.length < 2){
    messages.push('Please enter a valid name')
  }
  if(code.value==''){
    messages.push('Please select a phone code')
  }
  if(city.value==''){
    messages.push('Please select a city')
  }
  if(gender.value == ''){
    messages.push('Please select a gender')
  }
  if (!(document.getElementById("op1").checked ||                   
        document.getElementById("op2").checked || 
        document.getElementById("op3").checked || 
        document.getElementById("op4").checked || 
        document.getElementById("op5").checked || 
        document.getElementById("op6").checked)){messages.push('Please select relevent prior academic certification')}

}