[SUCS Devel] [Git][sucssite/sucs-site][master] 5 commits: Improve the regex and validation

Imran Hussain imranh at sucs.org
Wed Oct 14 08:49:35 BST 2015


Imran Hussain pushed to branch master at sucssite / sucs-site


Commits:
c508252e by gigosaurus at 2015-10-13T11:45:04Z
Improve the regex and validation

- - - - -
f77eef1c by gigosaurus at 2015-10-13T13:07:03Z
Merge branch 'master' of projects.sucs.org:/sucssite/sucs-site

- - - - -
9819ed53 by gigosaurus at 2015-10-13T19:01:10Z
postcodes can now be dumb

- - - - -
f28a63a1 by gigosaurus at 2015-10-13T20:52:33Z
removing my fuck up

- - - - -
b4a9420c by Imran Hussain at 2015-10-14T08:49:31Z
Merge branch 'master' into 'master'

Reduced user error

Postcodes can now be lowercase and lack a space.
Phone numbers must be at least 10 characters long (increased from 1, shouldn't cause any issues?).
Any email containing "@sucs." is not allowed for signup.
signup.js has been reformatted.

See merge request !33

- - - - -


5 changed files:

- components/signupajax.php
- htdocs/js/signup.js
- lib/sanitization.php
- lib/validation.php
- lib/validationData.php


Changes:

=====================================
components/signupajax.php
=====================================
--- a/components/signupajax.php
+++ b/components/signupajax.php
@@ -14,7 +14,10 @@ if (isset($_GET['key'])) {
             }
             break;
         case "postcode":
-            echo lookup_postcode($_GET['value']);
+            $postcode = validPostcode($_GET['value']);
+            if ($postcode != false) {
+                lookup_postcode($postcode);
+            }
             break;
         case "username":
             $username = $_GET['value'];


=====================================
htdocs/js/signup.js
=====================================
--- a/htdocs/js/signup.js
+++ b/htdocs/js/signup.js
@@ -1,275 +1,283 @@
-var validation = {"studentid":false, "username":false, "realname":false, "address":false, "contact":false, "email":false, "phone":false};
+var validation = {
+    "studentid": false,
+    "username": false,
+    "realname": false,
+    "address": false,
+    "contact": false,
+    "email": false,
+    "phone": false
+};
 var submitted = false;
-function validate(){
-	var valid = true;
-	for (field in req){
-		if (!validation[req[field]]){
-			valid=false;
-			break;
-		}
-	}
-	if((valid || ($('input#override:checked').size()==1)) && !submitted && ($('input#tnc:checked').size()==1)){
-		$('input#submit').removeAttr("disabled");
-		return true;
-	}
-	else{
-		$('input#submit').attr("disabled","disabled");
-		return false;
-	}
-	
+function validate() {
+    var valid = true;
+    for (field in req) {
+        if (!validation[req[field]]) {
+            valid = false;
+            break;
+        }
+    }
+    if ((valid || ($('input#override:checked').size() == 1)) && !submitted && ($('input#tnc:checked').size() == 1)) {
+        $('input#submit').removeAttr("disabled");
+        return true;
+    }
+    else {
+        $('input#submit').attr("disabled", "disabled");
+        return false;
+    }
+
 
 }
-function processPostcode(){
-		// lookup postcode
-                $.getJSON("signup/ajax",{key: "postcode",value: $('input#postcode').val()}, function(j){
-			//populate dropdown	
-                        var options = '';
-                        if (j.addresses.length > 1) {
-				// make dropdown visible
-                                $('div#addseldiv').removeAttr("style");
-                                for (var i = 0; i < (j.addresses.length); i++) {
-                                        options += '<option>';
-                                        if (j.addresses[i].flat != null) {
-                                        	options += j.addresses[i].flat + "\n";
-                                        }
-                                        if (j.addresses[i].house != null) {
-                                        	options += j.addresses[i].house + "\n";
-                                        }
-                                        if (j.addresses[i].road != null) {
-                                        	options += j.addresses[i].road + "\n";
-                                        }
-                                        if (j.addresses[i].city != null) {
-                                        	options += j.addresses[i].city + "\n";
-                                        }
-                                        options += '</option>';
-                                }
-                                $("select#addsel").html(options);
-                                $('select#addsel option:first').attr('selected', 'selected');
-                        }
-                        if (j.addresses.length == 1) {
-                                $('div#addseldiv').attr("style","display:none");
-                                $("textarea#address")
-				$('div#addressmessage').attr("style","color:green; float:right; clear:right;");
-				$('div#addressmessage').html("OK");
-				validation["address"]=true;
-				validate();
-                        }
-                })
+function processPostcode() {
+    // lookup postcode
+    $.getJSON("signup/ajax", {key: "postcode", value: $('input#postcode').val()}, function (j) {
+        //populate dropdown
+        var options = '';
+        if (j.addresses.length > 1) {
+            // make dropdown visible
+            $('div#addseldiv').removeAttr("style");
+            for (var i = 0; i < (j.addresses.length); i++) {
+                options += '<option>';
+                if (j.addresses[i].flat != null) {
+                    options += j.addresses[i].flat + "\n";
+                }
+                if (j.addresses[i].house != null) {
+                    options += j.addresses[i].house + "\n";
+                }
+                if (j.addresses[i].road != null) {
+                    options += j.addresses[i].road + "\n";
+                }
+                if (j.addresses[i].city != null) {
+                    options += j.addresses[i].city + "\n";
+                }
+                options += '</option>';
+            }
+            $("select#addsel").html(options);
+            $('select#addsel option:first').attr('selected', 'selected');
+        }
+        if (j.addresses.length == 1) {
+            $('div#addseldiv').attr("style", "display:none");
+            $("textarea#address")
+            $('div#addressmessage').attr("style", "color:green; float:right; clear:right;");
+            $('div#addressmessage').html("OK");
+            validation["address"] = true;
+            validate();
+        }
+    })
 }
 
-function lookupSID(setname){
-        $.get("/signup/ajax",{key:"sid" ,value: $('input#studentid').val()},function(j){
-                arr=j.split(":");
-                key=arr.shift();
-                val=arr.join(":");
-                if(key=="OK"){
-			if(setname=true)
-				$("input#realname").val(val);
-                        $('div#studentidmessage').attr("style","color:green; float:right; clear:right;");
-			$('div#studentidmessage').html(key);
-			validation['studentid']=true;
-			processName("realname","realname");
+function lookupSID(setname) {
+    $.get("/signup/ajax", {key: "sid", value: $('input#studentid').val()}, function (j) {
+        arr = j.split(":");
+        key = arr.shift();
+        val = arr.join(":");
+        if (key == "OK") {
+            if (setname = true)
+                $("input#realname").val(val);
+            $('div#studentidmessage').attr("style", "color:green; float:right; clear:right;");
+            $('div#studentidmessage').html(key);
+            validation['studentid'] = true;
+            processName("realname", "realname");
 
-                }
-                else{
-                        $('div#studentidmessage').attr("style","color:red; float:right; clear:right;");
-			$('div#studentidmessage').html(val);
-			validation['email']=false;
-			validate();
-                }
-        },'text')
+        }
+        else {
+            $('div#studentidmessage').attr("style", "color:red; float:right; clear:right;");
+            $('div#studentidmessage').html(val);
+            validation['email'] = false;
+            validate();
+        }
+    }, 'text')
 }
-function processSID(){
-	lookupSID(true);
+function processSID() {
+    lookupSID(true);
 }
 
-function processUsername(){
-        $.get("/signup/ajax",{key: "username", value: $('input#username').val()},function(j){
-                if (j!="OK") {
-                        $('div#usernamemessage').attr("style","color:red; float:right; clear:right;");
-			validation['username']=false;
-                }
-                else{   
-                        $('div#usernamemessage').attr("style","color:green; float:right; clear:right;");
-			validation['username']=true;
-                }
-		$('div#usernamemessage').html(j);
-		validate();
-        },'text')
+function processUsername() {
+    $.get("/signup/ajax", {key: "username", value: $('input#username').val()}, function (j) {
+        if (j != "OK") {
+            $('div#usernamemessage').attr("style", "color:red; float:right; clear:right;");
+            validation['username'] = false;
+        }
+        else {
+            $('div#usernamemessage').attr("style", "color:green; float:right; clear:right;");
+            validation['username'] = true;
+        }
+        $('div#usernamemessage').html(j);
+        validate();
+    }, 'text')
 }
 
-function processName(type, input){
-        $.get("/signup/ajax",{key: type, value: $('input#'+input).val()},function(j){
-                if (j!="OK") {
-                        $('div#'+input+'message').attr("style","color:red; float:right; clear:right;");
-			validation[input]=false;
-                }
-                else{   
-                        $('div#'+input+'message').attr("style","color:green; float:right; clear:right;");
-			validation[input]=true;
-                }
-		$('div#'+input+'message').html(j);
-		validate();
-        },'text')
+function processName(type, input) {
+    $.get("/signup/ajax", {key: type, value: $('input#' + input).val()}, function (j) {
+        if (j != "OK") {
+            $('div#' + input + 'message').attr("style", "color:red; float:right; clear:right;");
+            validation[input] = false;
+        }
+        else {
+            $('div#' + input + 'message').attr("style", "color:green; float:right; clear:right;");
+            validation[input] = true;
+        }
+        $('div#' + input + 'message').html(j);
+        validate();
+    }, 'text')
 }
-function processContact(){
-	processName('realname','contact');
+function processContact() {
+    processName('realname', 'contact');
 }
 
-function processAddress(){
-        $.get("/signup/ajax",{key: "address", value: $('textarea#address').val()},function(j){
-                if (j!="OK") {
-                        $('div#addressmessage').attr("style","color:red; float:right; clear:right;");
-                        validation['address']=false;
-                }   
-                else{   
-                        $('div#addressmessage').attr("style","color:green; float:right; clear:right;");
-                        validation['address']=true;
-                }
-		$('div#addressmessage').html(j);
-		validate();
-        },'text')
+function processAddress() {
+    $.get("/signup/ajax", {key: "address", value: $('textarea#address').val()}, function (j) {
+        if (j != "OK") {
+            $('div#addressmessage').attr("style", "color:red; float:right; clear:right;");
+            validation['address'] = false;
+        }
+        else {
+            $('div#addressmessage').attr("style", "color:green; float:right; clear:right;");
+            validation['address'] = true;
+        }
+        $('div#addressmessage').html(j);
+        validate();
+    }, 'text')
 }
 
-function processEmail(){
-        $.get("/signup/ajax",{key: "email", value: $('input#email').val()},function(j){
-                if (j!="OK") {
-                        $('div#emailmessage').attr("style","color:red; float:right; clear:right;");
-			validation['email']=false;
-                }
-                else{   
-                        $('div#emailmessage').attr("style","color:green; float:right; clear:right;");
-			validation['email']=true;
-                }
-		$('div#emailmessage').html(j);
-		validate();
-        },'text')
+function processEmail() {
+    $.get("/signup/ajax", {key: "email", value: $('input#email').val()}, function (j) {
+        if (j != "OK") {
+            $('div#emailmessage').attr("style", "color:red; float:right; clear:right;");
+            validation['email'] = false;
+        }
+        else {
+            $('div#emailmessage').attr("style", "color:green; float:right; clear:right;");
+            validation['email'] = true;
+        }
+        $('div#emailmessage').html(j);
+        validate();
+    }, 'text')
 }
 
-function processPhone(){
-        $.get("/signup/ajax",{key: "phone", value: $('input#phone').val()},function(j){
-                if (j!="OK") {
-                        $('div#phonemessage').attr("style","color:red; float:right; clear:right;");
-			validation['phone']=false;
-                }
-                else{   
-                        $('div#phonemessage').attr("style","color:green; float:right; clear:right;");
-			validation['phone']=true;
-                }
-		$('div#phonemessage').html(j);
-		validate();
-        },'text')
+function processPhone() {
+    $.get("/signup/ajax", {key: "phone", value: $('input#phone').val()}, function (j) {
+        if (j != "OK") {
+            $('div#phonemessage').attr("style", "color:red; float:right; clear:right;");
+            validation['phone'] = false;
+        }
+        else {
+            $('div#phonemessage').attr("style", "color:green; float:right; clear:right;");
+            validation['phone'] = true;
+        }
+        $('div#phonemessage').html(j);
+        validate();
+    }, 'text')
 }
 
-$(function(){
-	if($('input#studentid').size()==1){
-		usertype=1;
-	}
-	else if($('input#contact').size()==1){
-		usertype=2;
-	}
-	else{
-		usertype=5;
-	}
-        req=new Array("username","realname","email","phone");
-        switch(usertype){
-                case "1":
-                        req.push("studentid","address");
-                        break;
-                case "2":
-                        req.push("contact");
-                        break;
-                case "5":
-                        req.push("address");
+$(function () {
+    if ($('input#studentid').size() == 1) {
+        usertype = 1;
+    }
+    else if ($('input#contact').size() == 1) {
+        usertype = 2;
+    }
+    else {
+        usertype = 5;
+    }
+    req = new Array("username", "realname", "email", "phone");
+    switch (usertype) {
+        case "1":
+            req.push("studentid", "address");
+            break;
+        case "2":
+            req.push("contact");
+            break;
+        case "5":
+            req.push("address");
+    }
+    //usertype=$('input#usertype').val();
+    $("document").ready(function () {
+        // makes script sutff appear
+        $('div#postcodediv').removeAttr("style");
+        $('input#submit').attr("disabled", "disabled");
+        // if the fields are not empty validate them
+        if ($('input#username').val() != "") processUsername();
+        if ($('input#email').val() != "") processEmail();
+        if ($('input#phone').val() != "") processPhone();
+        if (usertype != 2) {
+            if ($('input#postcode').val() != "") processPostcode();
+            if ($('textarea#address').val() != "") processAddress();
+            if ($('input#realname').val() != "") processName("realname", "realname");
+        }
+        else {
+            if ($('input#contact').val() != "") processContact();
+            if ($('input#realname').val() != "") processName("socname", "realname");
         }
-	//usertype=$('input#usertype').val();
-	$("document").ready(function(){
-		// makes script sutff appear
-		$('div#postcodediv').removeAttr("style");
-		$('input#submit').attr("disabled","disabled");
-		// if the fields are not empty validate them
-		if($('input#username').val()!="") processUsername();
-		if($('input#email').val()!="") processEmail();
-		if($('input#phone').val()!="") processPhone();
-		if(usertype!=2){
-			if($('input#postcode').val()!="") processPostcode();
-			if($('textarea#address').val()!="") processAddress();
-			if($('input#realname').val()!="") processName("realname","realname");
-		}
-		else{
-			if($('input#contact').val()!="") processContact();
-			if($('input#realname').val()!="") processName("socname","realname");
-		}
-		if((usertype==1) && ($('input#studentid').val()!="")){
-			if($('input#realname').val()=="") lookupSID(true);
-			else lookupSID(false);
-		}
+        if ((usertype == 1) && ($('input#studentid').val() != "")) {
+            if ($('input#realname').val() == "") lookupSID(true);
+            else lookupSID(false);
+        }
+
+        validate();
+    })
+    // dont do address stuff for societies
+    if (usertype != 2) {
+        // process postcode when the box changes
+        $("input#postcode").change(processPostcode);
 
-		validate();
-	})
-	// dont do address stuff for societies
-	if(usertype!=2){
-	// process postcode when the box changes
-	$("input#postcode").change(processPostcode);
-	
-	//populate the address box when an address is selected
-	$("select#addsel").change(function(){
-	
-		// everything is shit and will strip out the line breaks
-		// so we have to do weird shit with innerHTML
-	
-		// store the entire options as e
-		var e = document.getElementById("addsel");
-		// store the selected address's innerHTML as strUser (because stackpver flow copy and paste)
-		var strUser = e.options[e.selectedIndex].innerHTML;
-		// set the text area to the strUser var
-		$("textarea#address").text( strUser );
-		$('div#addressmessage').attr("style","color:green; float:right; clear:right;");
-		$('div#addressmessage').html("OK");
-		validation['address']=true;
-	})
-	}
-	//else deal with the contact field
-	else{
+        //populate the address box when an address is selected
+        $("select#addsel").change(function () {
+
+            // everything is shit and will strip out the line breaks
+            // so we have to do weird shit with innerHTML
+
+            // store the entire options as e
+            var e = document.getElementById("addsel");
+            // store the selected address's innerHTML as strUser (because stackpver flow copy and paste)
+            var strUser = e.options[e.selectedIndex].innerHTML;
+            // set the text area to the strUser var
+            $("textarea#address").text(strUser);
+            $('div#addressmessage').attr("style", "color:green; float:right; clear:right;");
+            $('div#addressmessage').html("OK");
+            validation['address'] = true;
+        })
+    }
+    //else deal with the contact field
+    else {
         $("input#contact").change(processContact)
 
-	}
-	//if is a student
-	if(usertype==1){
-	//lookup the real name from the studentid
-	$("input#studentid").change(processSID)
-	}
-	//validate username
-	$("input#username").change(processUsername)
-	//validate personal and society names differntly
-	if(usertype!=2){
-		persoc='realname';
-	}
-	else{
-		persoc='socname';
-	}
-	//validate real name
-	$("input#realname").change(function(){
-		processName(persoc, 'realname');
-	})
-	
-        //validate email address
-        $("input#email").change(processEmail);
-        //validate phone number
-        $("input#phone").change(processPhone);
-	$("textarea#address").change(processAddress);
-	$("input#override").change(validate);
-	$("input#tnc").change(validate);
-	// Disable the submit button once clicked
-	$("form#mainform").submit(function() {
-		var valid = validate();
-		if(valid){
-			// mark form as submitted
-			submitted = true;
-			// disable the submit button
-			$('input#submit').attr("disabled","disabled");
-		}
-		return valid;
-	});
+    }
+    //if is a student
+    if (usertype == 1) {
+        //lookup the real name from the studentid
+        $("input#studentid").change(processSID)
+    }
+    //validate username
+    $("input#username").change(processUsername)
+    //validate personal and society names differntly
+    if (usertype != 2) {
+        persoc = 'realname';
+    }
+    else {
+        persoc = 'socname';
+    }
+    //validate real name
+    $("input#realname").change(function () {
+        processName(persoc, 'realname');
+    })
+
+    //validate email address
+    $("input#email").change(processEmail);
+    //validate phone number
+    $("input#phone").change(processPhone);
+    $("textarea#address").change(processAddress);
+    $("input#override").change(validate);
+    $("input#tnc").change(validate);
+    // Disable the submit button once clicked
+    $("form#mainform").submit(function () {
+        var valid = validate();
+        if (valid) {
+            // mark form as submitted
+            submitted = true;
+            // disable the submit button
+            $('input#submit').attr("disabled", "disabled");
+        }
+        return valid;
+    });
 })
 


=====================================
lib/sanitization.php
=====================================
--- a/lib/sanitization.php
+++ b/lib/sanitization.php
@@ -9,4 +9,12 @@ function sanitizeAddress($address)
     return str_replace(array("\r\n", "\r"), array("\n", "\n"), $address);
 }
 
+function sanitizePostcode($postcode)
+{
+    //force uppercase, remove any possible stupid spaces and add the single space in the correct place
+    $postcode = strtoupper($postcode);
+    $postcode = str_replace(" ", "", $postcode);
+    return substr_replace($postcode, " ", -3, 0);
+}
+
 ?>


=====================================
lib/validation.php
=====================================
--- a/lib/validation.php
+++ b/lib/validation.php
@@ -252,11 +252,22 @@ function validAddress($address)
     }
 }
 
+function validPostcode($postcode)
+{
+    $postcode = sanitizePostcode($postcode);
+    if (!preg_match('/^[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} [0-9][A-Z]{2}$/', $postcode)) {
+        return false;
+    } else {
+        return $postcode;
+    }
+
+}
+
 function validPhone($phone)
 {
     global $error;
     $phone = sanitizePhone($phone);
-    if (!preg_match("/^\+?[0-9-]+$/", $phone)) {
+    if (!preg_match("/^\+?[0-9-]{10,}$/", $phone)) {
         $error = "Must be all numbers";
         return false;
     }
@@ -266,7 +277,7 @@ function validPhone($phone)
 function validSignupEmail($email)
 {
     global $error;
-    if (preg_match('/@sucs\.org$/', $email)) {
+    if (preg_match('/@sucs\.$/', $email)) {
         $error = "SUCS email addresses are not allowed";
         return false;
     } elseif (!validEmail($email)) {


=====================================
lib/validationData.php
=====================================
--- a/lib/validationData.php
+++ b/lib/validationData.php
@@ -1,5 +1,5 @@
 <?
-// lookup real names from sid's useing campus ldap
+// lookup real names from sid's using campus ldap
 function lookupSID($sid)
 {
     $ds = ldap_connect("ccs-suld1.swan.ac.uk");
@@ -12,7 +12,7 @@ function lookupSID($sid)
     return ucwords(strtolower($info[0]['givenname'][0] . " " . $info[0]['sn'][0]));
 }
 
-// lookup addresses from postcodes useing the univeritys website
+// lookup addresses from postcodes using the university's website
 function lookup_postcode($postcode)
 {
 
@@ -21,9 +21,8 @@ function lookup_postcode($postcode)
     $url = "http://paf.sucs.org/?apikey=$apikey&postcode=" . rawurlencode($postcode);
 
     $req = curl_init($url);
-    $page = curl_exec($req);
+    curl_exec($req);
     curl_close($req);
-
 }
 
 ?>



View it on GitLab: https://projects.sucs.org/sucssite/sucs-site/compare/a87d2d7631f6bd6e3ffffb0fdea398e2f0df31f4...b4a9420ca420b07057df06904623ac939fd45c49
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/devel/attachments/20151014/cf9496c9/attachment-0001.html>


More information about the Devel mailing list