< form id = "signupForm" method = "post" >
< div class = "form-group" >
< label class = "form-label" > Full name</ label >
< input name = "fullName" type = "text" class = "form-input" />
</ div >
< div class = "form-group" >
< label class = "form-label" > Username</ label >
< input name = "username" type = "text" class = "form-input" />
</ div >
< div class = "form-group" >
< label class = "form-label" > Email address</ label >
< input name = "email" type = "text" class = "form-input" />
</ div >
< div class = "form-group" >
< label class = "form-label" > Password</ label >
< input name = "password" type = "password" class = "form-input" />
</ div >
< div class = "form-group" >
< label class = "form-label" > Gender</ label >
< label class = "form-radio" >
< input name = "gender" type = "radio" value = "male" />
< i class = "form-icon" ></ i > Male
</ label >
< label class = "form-radio" >
< input name = "gender" type = "radio" value = "female" />
< i class = "form-icon" ></ i > Female
</ label >
< label class = "form-radio" >
< input name = "gender" type = "radio" value = "other" />
< i class = "form-icon" ></ i > Other
</ label >
</ div >
< div class = "form-group" >
< label class = "form-label" >< span id = "captchaOperation" ></ span ></ label >
< input name = "captcha" type = "password" class = "form-input" />
</ div >
< div class = "form-group" >
< label class = "sr-only" ></ label >
< label class = "form-checkbox" >
< input name = "agree" type = "checkbox" />
< i class = "form-icon" ></ i > Agree with the terms and conditions
</ label >
</ div >
< div class = "form-group" >
< button type = "submit" class = "btn btn-primary" > Sign up</ button >
</ div >
</ form >
< script >
$ ( document ). ready ( function () {
// Generate a simple captcha
function randomNumber ( min , max ) {
return Math . floor ( Math . random () * ( max - min + 1 ) + min );
};
$ ( '#captchaOperation' ). html ([ randomNumber ( 1 , 100 ), '+' , randomNumber ( 1 , 200 ), '=' ]. join ( ' ' ));
$ ( '#signupForm' ). formValidation ({
framework : 'spectre' ,
icon : {
valid : 'fa fa-check' ,
invalid : 'fa fa-times' ,
validating : 'fa fa-refresh' ,
feedback : 'fv-control-feedback'
},
fields : {
fullName : {
validators : {
notEmpty : {
message : 'The full name is required'
}
}
},
username : {
validators : {
notEmpty : {
message : 'The username is required'
},
stringLength : {
min : 6 ,
max : 30 ,
message : 'The username must be more than 6 and less than 30 characters long'
},
regexp : {
regexp : /^[a-zA-Z0-9_\.]+$/ ,
message : 'The username can only consist of alphabetical, number, dot and underscore'
}
}
},
email : {
validators : {
notEmpty : {
message : 'The email address is required'
},
emailAddress : {
message : 'The input is not a valid email address'
}
}
},
password : {
validators : {
notEmpty : {
message : 'The password is required'
},
different : {
field : 'username' ,
message : 'The password cannot be the same as username'
}
}
},
gender : {
validators : {
notEmpty : {
message : 'The gender is required'
}
}
},
captcha : {
validators : {
callback : {
message : 'Wrong answer' ,
callback : function ( value , validator ) {
var items = $ ( '#captchaOperation' ). html (). split ( ' ' ), sum = parseInt ( items [ 0 ]) + parseInt ( items [ 2 ]);
return value == sum ;
}
}
}
},
agree : {
validators : {
notEmpty : {
message : 'You must agree with the terms and conditions'
}
}
}
}
});
});
</ script >