Validate Form
#\controllers\customer.py
import config, re, json
from bottle import template, route, request, redirect

@route('/customer')
def displayCustomer():
  return template('customer', data=config.kargs)

@route('/custform')
def getCustomerForm():
  return template('custform', data=config.kargs)
  
@route('/customer', method="POST")
def getFormData():
  name = request.forms.get("fname")
  phone = request.forms.get("fphone")

  if not re.match(r"^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$", phone):
    config.kargs['message'] = "You need to enter a right phone number."
    return template('custform', data=config.kargs)

  else:
    redirect('/customer')


%include("./partials/header.tpl")



CUSTOMER ENTRY FORM
Name: Phone:

{{data['message']}}

%data['message'] = ""
%include("./partials/footer")
//\public\js\main.js
class Bicycle{

  customerForm(formId){
    var name = document.forms[formId]['fname'].value;
    var phone = document.forms[formId]['fphone'].value;
  
    if((name == "") || (phone == "")){
      alert("Please fill this form with your name and phone number!");
      return false;
    }else{
      var phoneRegex = /^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;
      var numberResult = phoneRegex.test(phone);
      if(!numberResult){
        alert("Please fill this form with the right phone number.");
        return false;
      }
    }
  }
  
}//end of class

var bicycle = new Bicycle();

GitHub: https://github.com/Sokhavuth/Rental-Bicycle-App
Heroku: https://khmerweb-rba.herokuapp.com/