form validation on submit - forms

I am facing problem in form validation. Following is my jQuery validation code.
kindly help me how this validation is working on submit button.
<script type="text/javascript">
$(document).ready(function() {
$(".col a").click(function() {
$(".col a").removeClass("active");
$(this).addClass("active");
});
});
jQuery(document).ready(function() {
jQuery(".expContent").hide();
//toggle the componenet with class msg_body
jQuery(".expHeading").click(function() {
jQuery(this).next(".expContent").slideToggle(500);
});
});
$(document).ready(function() {
// Vertical
$("#vertical").on("blur", function(e) {
if ($("#vertical").val().length < 2) {
alert("vertical", "Vertical is Mandatory");
} else {
hideMsg("vertical");
}
});
// Name
$("#name").on("blur", function(e) {
if ($("#name").val().length < 2) {
alert("Name is Mandatory");
} else {
hideMsg("name");
}
});
function IsEmail(email) {
var filter = /^[\w-\.]+#([\w-]+\.)+[\w-]{2,4}$/;
if (filter.test(email)) {
return true;
} else {
return false;
}
}
$("#email").on("blur", function(e) {
if ($("#email").val().length == 0) {
//alert("Please submit a Valid Email Id");
}
if (IsEmail($("#email").val())) {
hideMsg("email");
} else {
alert("Please submit a Valid Email Id");
}
});
// Mobile No
$("#enqMobileNo").on("blur", function(e) {
if ($("#enqCountryResidence").val() == "in") {
if ($("#enqMobileNo").val().length == 10) {
hideMsg("enqMobileNo");
} else {
alert('Please Enter 10 Digit Mobile No. Only like 9812345678. Without Area or Country Code i.e "0" or "+91"');
}
} else {
if ($("#enqMobileNo").val().length 1) {
hideMsg("enqMobileNo");
} else {
alert("Please Enter Mobile No. Only. Without Area or Country Code");
}
}
});
$("#enqMobileNo").on('keyup', function() {
if ($("#enqMobileNo").val() == "0") {
$("#enqMobileNo").val("");
}
if ($("#enqCountryResidence").val() == "in") {
limitText(this, 10);
if ($("#enqMobileNo").val().length == 10) {
hideMsg("enqMobileNo");
}
} else {
//inlineMsg
("enqMobileNo", "Please Enter Mobile No. Only<br /Without Area or Country Code");
}
});
// Gender
$("#gender").on("blur", function(e) {
if ($("#gender").val() == "") {
alert('Please select Gender', 2);
} else {
hideMsg("gender");
}
});
// Age
$("#age").on("blur", function(e) {
if ($("#age").val() == "") {
alert('Please select Age', 2);
} else {
hideMsg("age");
}
});
// City
$("#enqCity").on("blur", function(e) {
if ($("#enqCity").val() == "") {
alert('Current Location City Name is Mandatory', 2);
} else {
hideMsg("enqCity");
}
});
// Course
$("#enqSection").on("blur", function(e) {
if ($("#enqSection").val() == "") {
alert('Please Select Course', 2);
} else {
hideMsg("enqSection");
}
});
// Spl
$("#enqSpeciality").on("blur", function(e) {
if ($("#enqSpeciality").val() == "") {
alert('Please Select Speciality', 2);
} else {
hideMsg("enqSpeciality");
}
});
// Level
$("#enqLevel").on("blur", function(e) {
if ($("#enqLevel").val() == "") {
alert('Please Select Level', 2);
} else {
hideMsg("enqLevel");
}
});
function limitText(field, maxChar) {
var ref = $(field),
val = ref.val();
if (val.length = maxChar) {
ref.val(function() {
console.log(val.substr(0, maxChar))
return val.substr(0, maxChar);
});
}
}
});
</script>

I think it is better to use jQuery form validation plugin. It is very easy to use.You can get step by step help with examples on this site

Related

SIPml5 one sided voice

Below is the code of my dialer. I can register and connect calls successfully with the below code. But, after call is connected only the other end (non sipml5) can hear voice. But, the sipml5 side can not hear anything.However, I could connect and pass voice using the sipml5 client from sipml5 website(sipml5.org/call.htm). I must be doing something wrong, but cant figure out what.
<script src="api/SIPml-api.js" type="text/javascript"> </script>
<script type="text/javascript">
var readyCallback = function(e){
createSipStack(); // see next section
};
var errorCallback = function(e){
onsole.error('Failed to initialize the engine: ' + e.message);
}
SIPml.init(readyCallback, errorCallback);
var sipStack;
var callSession;
function eventsListener(e){
console.info('Change of status|Server response: '+e.type+':'+e.message+':'+e.
session+':'+e.description);
if(e.type == 'started'){
login();
}
else if(e.type == 'i_new_message'){ // incoming new SIP MESSAGE (SMS-like)
acceptMessage(e);
}
else if(e.type == 'i_new_call'){ // incoming audio/video call
if(confirm("Incomming Call Request! Do you accept?")){
acceptCall(e);
}else{
e.newSession.reject()
}
}
else if(e.type == 'connected'){
if(e.session == registerSession){
setStatus(e.type,'Registered...');
}else{
setStatus(e.type,e.description);
}
}
else if(e.type == 'i_ao_request' && e.description == 'Ringing' ){
document.getElementById('call').value = 'End Call';
setStatus(e.type,e.description);
}
else if(e.type == 'terminated' || e.type == 'terminating'){
if(e.session == registerSession){
setStatus('Unable to Register');
}else{
setStatus(e.type,e.description);
}
}
}
function createSipStack(){
sipStack = new SIPml.Stack({
realm: 'foo.bar.com',
impi: 'usertest',
impu: 'sip:usertest#foo.bar.com',
password: '1234',
display_name: 'alice',
websocket_proxy_url: 'ws://11.11.11.0:8080',
enable_rtcweb_breaker: false,
events_listener: { events: '*', listener: eventsListener },
sip_headers: [ // optional
{ name: 'User-Agent', value: 'IM-client/OMA1.0 sipML5-v1.0.0.0' },
{ name: 'Organization', value: 'SuperCops.us' }
]
}
);
}
sipStack.start();
function login(){
registerSession = sipStack.newSession('register', {
events_listener: { events: '*', listener: eventsListener } // optional: '*' means all events
});
registerSession.register();
}
function makeCall(){
var number = document.getElementById('number').value;
if(number == ''){
alert('No number entered');
}
else if(document.getElementById('call').value == 'End Call'){
callSession.hangup();
}else{
setStatus('Trying','Trying to call:'+numberFilter(number));
callSession = sipStack.newSession('call-audio',{
events_listener: { events: '*', listener: eventsListener }
});
callSession.call(numberFilter(number));
}
}
function acceptCall(event){
callSession = event.newSession;
/*('accept',{
events_listener: { events: '*', listener: eventsListener }
});*/
callSession.accept();
eventsListener(callSession);
setStatus('connected','In Call');
}
function setStatus(type,status){
document.getElementById('status').innerHTML = status;
if(type == 'terminated' || type == 'terminating'){
document.getElementById('call').value = 'Call';
}else if(status == 'Ringing' || status == 'Ringing' || status == 'In Call' || type == 'Trying'){
document.getElementById('call').value = 'End Call';
}
}
function numberFilter(number){
return number;
}

Leaflet.js How to stop on map evnet

I am calling a function when the user has panned the map with:
$('#updateTheMap').click(function() {
if (document.getElementById('updateMap').checked) {
// stop the the dragend event...
}
else {
map.on('dragend', function() {
sortBinis(simpleFilterSql);
});
}
});
But I can not figure out how to end this event?
Use a variable:
var updateTheMap = true;
$('#updateTheMap').click(function() {
updateTheMap = document.getElementById('updateMap').checked;
});
map.on('dragend', function() {
if (updateTheMap) sortBinis(simpleFilterSql);
});
If by 'end this event' you mean 'remove the event listener' you can use map.off()
function onDragend(e) { sortBinis(simpleFilterSql); }
$('#updateTheMap').click(function() {
if (document.getElementById('updateMap').checked) {
map.off('dragend', onDragend);
}
else {
map.on('dragend', onDragend);
}
});

Facebook API - How to get user's nationality (location)?

I'm using Facebook Phonegap plugin for my mobile game project. My problem is I can't get user's location after call facebookConnectPlugin:
facebookConnectPlugin.login(["email"], function(response) {
console.log(JSON.stringify(response.authResponse.userID));
sessionStorage.setItem("userID", JSON.stringify(response.authResponse.userID));
facebookConnectPlugin.api(
"me/?fields=id,email,first_name,last_name,location", ["user_birthday"],
function(response) {
alert(JSON.stringify(response))
}, function(response) {
alert(JSON.stringify(response))
});
$state.transitionTo('app.home');
}, function(response) {
alert(JSON.stringify(response))
});
facebookConnectPlugin.js
/*
* #author Ally Ogilvie
* #copyright Wizcorp Inc. [ Incorporated Wizards ] 2014
* #file - facebookConnectPlugin.js
* #about - JavaScript interface for PhoneGap bridge to Facebook Connect SDK
*
*
*/
if (!window.cordova) {
// This should override the existing facebookConnectPlugin object created from cordova_plugins.js
var facebookConnectPlugin = {
getLoginStatus: function (s, f) {
// Try will catch errors when SDK has not been init
try {
FB.getLoginStatus(function (response) {
s(response);
});
} catch (error) {
if (!f) {
console.error(error.message);
} else {
f(error.message);
}
}
},
showDialog: function (options, s, f) {
if (!options.name) {
options.name = "";
}
if (!options.message) {
options.message = "";
}
if (!options.caption) {
options.caption = "";
}
if (!options.description) {
options.description = "";
}
if (!options.link) {
options.link = "";
}
if (!options.picture) {
options.picture = "";
}
// Try will catch errors when SDK has not been init
try {
FB.ui({
method: options.method,
message: options.message,
name: options.name,
caption: options.caption,
description: (
options.description
),
link: options.link,
picture: options.picture
},
function (response) {
if (response && response.request) {
s(response);
} else {
f(response);
}
});
} catch (error) {
if (!f) {
console.error(error.message);
} else {
f(error.message);
}
}
},
// Attach this to a UI element, this requires user interaction.
login: function (permissions, s, f) {
// JS SDK takes an object here but the native SDKs use array.
var permissionObj = {};
if (permissions && permissions.length > 0) {
permissionObj.scope = permissions.toString();
}
FB.login(function (response) {
if (response.authResponse) {
s(response);
} else {
f(response.status);
}
}, permissionObj);
},
getAccessToken: function (s, f) {
var response = FB.getAccessToken();
if (!response) {
if (!f) {
console.error("NO_TOKEN");
} else {
f("NO_TOKEN");
}
} else {
s(response);
}
},
logEvent: function (eventName, params, valueToSum, s, f) {
// AppEvents are not avaliable in JS.
s();
},
logPurchase: function (value, currency, s, f) {
// AppEvents are not avaliable in JS.
s();
},
logout: function (s, f) {
// Try will catch errors when SDK has not been init
try {
FB.logout( function (response) {
s(response);
})
} catch (error) {
if (!f) {
console.error(error.message);
} else {
f(error.message);
}
}
},
api: function (graphPath, permissions, s, f) {
// JS API does not take additional permissions
// Try will catch errors when SDK has not been init
try {
FB.api(graphPath, function (response) {
if (response.error) {
f(response);
} else {
s(response);
}
});
} catch (error) {
if (!f) {
console.error(error.message);
} else {
f(error.message);
}
}
},
// Browser wrapper API ONLY
browserInit: function (appId, version) {
if (!version) {
version = "v2.0";
}
FB.init({
appId : appId,
cookie : true,
xfbml : true,
version : version
})
}
};
// Bake in the JS SDK
(function () {
console.log("launching FB SDK")
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/sdk.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
}
You don't ask for the appropriate permission user_location, and if you don't, you can't get access to the data. That's plain simple.
See https://developers.facebook.com/docs/graph-api/reference/v2.1/user/#readperms

unique form field validation in extjs4

Is there a cleaner way to define unique form field in extjs. Below is a sample code that is checking on client UID on client creation/edition. This code is working but has some bugs - for example on client creation if you enter a value that is already present in DB validator returns true until you unfocus the field.
Ext.define('AM.view.client.UniqueField', {
extend: 'Ext.form.field.Text',
alias : 'widget.uniquefield',
vtype: 'UniqueUid',
initComponent: function() {
Ext.apply(Ext.form.field.VTypes, {
UniqueUidMask : /[0-9]/i,
UniqueUid : function(val,field) {
if (val.length < 9) {
Ext.apply(Ext.form.field.VTypes, {
UniqueUidText: 'Company ID is too small'
});
return false;
} else {
var paste=/^[0-9_]+$/;
if (!paste.test(val)) {
Ext.apply(Ext.form.field.VTypes, {
UniqueUidText: 'Ivalid characters'
});
return false;
} else {
var mask = new Ext.LoadMask(field.up('form'),{msg:'Please wait checking....'});
mask.show();
var test= 0;
var store = Ext.create('AM.store.Clients');
store.load({params:{'uid':val, 'id': Ext.getCmp('client_id').getValue()}});
store.on('load', function(test) {
mask.hide();
if(parseInt(store.getTotalCount())==0){
this.uniqueStore(true);
}else{
Ext.apply(Ext.form.field.VTypes, {
UniqueUidText: 'Company ID is already present'
});
this.uniqueStore(false);
}
},this)
return true;
}
}}
},this);
this.callParent(arguments);
},
uniqueStore: function(is_error){
Ext.apply(Ext.form.field.VTypes, {
UniqueUidMask : /[0-9]/i,
UniqueUid : function(val,field) {
if (val.length < 9) {
Ext.apply(Ext.form.field.VTypes, {
UniqueUidText: 'Company ID is too small'
});
return false;
} else {
var paste=/^[0-9_]+$/;
if (!paste.test(val)) {
Ext.apply(Ext.form.field.VTypes, {
UniqueUidText: 'Ivalid characters'
});
return false;
} else {
var mask = new Ext.LoadMask(field.up('form'),{msg:'Please wait checking....'});
mask.show();
var store = Ext.create('AM.store.Clients');
store.load({params:{'uid':val, 'id': Ext.getCmp('client_id').getValue()}});
store.on('load', function(test) {
mask.hide();
if(parseInt(store.getTotalCount())==0){
this.uniqueStore(true);
}else{
this.uniqueStore(false);
}
},this)
return is_error;
}
}}
},this);
}
});
How about using server side validation?
I answered to similar issue here: extjs4 rails 3 model validation for uniqueness
Obviously you can change it to use "ajax" instead of "rest" proxy.

smart way to rewrite this function

I have this, and I am showing a div if user clicked one button and not showing it if the user clicked other. Its working but its dumb to do this way with repeatition
$j(document).ready(function() {
$j('#Button1').click( function () {
var data = $j("form").serialize();
$j.post('file.php', data, function(response){
$j("#Response").show();
});
});
$j('#Button21').click( function () {
var data = $j("form").serialize();
$j.post('file.php', data, function(response){
//do something else
});
});
});
I'd do it by adding a class to the selected buttons and then pull the event.target id from the click function:
$j('.buttons').click(function(e) {
var buttonId = e.target.id,
data = $j("form").serialize();
$j.post('file.php', data, function(response) {
switch (buttonId) {
case "Button1":
$j("#Response").show();
break;
case "Button21":
//do something else
break;
}
});
});
You need to abstract the data from the functionality.
sendClick('#Button1', function() {
$j('#Response').show();
});
sendClick('#Button21', function() {
// do something
});
sendClick function
function sendClick(selector, callback)
{
$j(selector).click( function () {
var data = $j("form").serialize();
$j.post('file.php', data, callback);
});
}
This way you can repeat the same functionality over and over by changing the selector and the callback. You could customise this even further by:
function sendClick(selector, options, callback)
{
// handle arguments
if(typeof options == 'function') {
callback = options;
options = {};
} else {
options = options || {};
}
$j.extend({
form: 'form',
file: 'file.php'
}, options);
// abstracted logic
$j(selector).click(function() {
var data = $j(options.form).serialize();
$j.post(options.file, data, callback);
});
}
then use like
sendClick('#select', {form: '#anotherForm'}, function() {
// do something
});
or
sendClick('#another', function(response) {
// something else
});
You can attach the event to both, and then, when you need to check which element triggered the event, use event.target.
$j(function() {
$j('#Button1, #Button2').click( function (event) {
var data = $j("form").serialize();
$j.post('file.php', data, function(response){
if ($(event.target).is('#Button1')) {
$j("#Response").show();
} else {
// Do something else
}
});
});
});
Here are two different ways:
You can combine the two handlers into one handler:
$j(document).ready(function () {
$j('#Button1, #Button21').click(function() {
var id = this.id;
var data = $j("form").serialize();
$j.post('file.php', data, function(response) {
if (id == 'Button1') {
// Show
} else {
// Do something else
}
});
});
});
Or write a special kind of handler:
$j.fn.clickAndPost = function (handler) {
this.click(function () {
var me = this;
var data = $j("form").serialize();
$j.post('file.php', data, function(response) {
handler.call(me);
});
});
});
...and attach two of them:
$j(document).ready(function () {
$j('#Button1').clickAndPost(function () {
// Show
});
$j('#Button21').clickAndPost(function () {
// Do something else
});
});
$j(function($) {
$('#Button1', '#Button21').click(function() {
var that = this,
data = $('form').serialize();
$.post('file.php', data, function(response) {
if ( that.id === 'Button1' ) {
$('#Response').show();
} else {
//do something else
}
});
});
});
$(document).ready(function() {
$('#Button1 #Button21').click(function() {
var that = this.attr("id");
data = $('form').serialize();
$.post('file.php', data, function(response) {
if ( that === 'Button1' ) {
$('#Response').show();
} else {
//do something else
}
});
});
});
Let me know if it's not working.