trying to create form validation - javascript

trying to create form validation to validate so that only alphanumeric characters will be allowed to submit using javascript but nothings working and i dont know what to do next, no jquery just plain js
function validate() {
'use strict';
var errMsg = "";
var result = true;
var fname = document.getElementById('fname').value;
if (!fname.match(/^[a-zA-Z]+$/)) {
errMsg = errMsg + "Alphanumeric Characters only\n";
result = false;
}
if (errMsg !== "") {
alert(errMsg);
}
return result;
}
function init() {
var enquiries = document.getElementById('enquiries').value;
enquiries.onsubmit = validate;
}
window.onload = init;

var enquiries = document.getElementById('enquiries').value;
enquiries.onsubmit = validate;
.onsubmit is being assigned to the value of the enquiries element, rather than the element itself. If you remove .value, your code should work.
plunker: http://plnkr.co/edit/SL7lZzLSje7BEuDRttym?p=preview

Related

Pass parameter in method

I have this code
var txt;
function change(){
txt = "#Utils.GeneratePass()";
document.getElementById("password_field").value = txt;
}
function changeToHash(txt){
alert(window.txt);
var password = "#Utils.Hash(txt)";
document.getElementById("password_field").value = password;
}
In method #Utils.Hash i need pass one string, and i want put the value of txt.
But i got the error "The name 'txt' does not exist in the current context.
You cannot pass js variable to C# code,you can try to pass #Utils.GeneratePass() to #Utils.Hash():
var txt;
function change(){
txt = "#Utils.GeneratePass()";
document.getElementById("password_field").value = txt;
}
function changeToHash(txt){
alert(window.txt);
var password = "#Utils.Hash(Utils.GeneratePass())";
document.getElementById("password_field").value = password;
}

Javascript: User Authentication JSON Error

I'm making a login page for my web application, and I'm using a temporary data storage (I know this is not safe) for user verifiation. I'm trying to compate the username input to the data (only correct email is needed at the moment to get it working), but it's not working at all. I'm not getting an error message, just the alert that the username is not correct. It now displays both the user input and data, so I can see that my input is correct. What's wrong with it?
Thanks in advance!
(The data/object is in a seperate js file)
var count = 2;
function validate() {
var un = document.login.username.value; /* Username Input variable*/
var pw = document.login.password.value; /* Password Input variable*/
var valid = false;
let data = responseData;
for(var account in data.accounts){
if( un == account.responseData.authenticatUser.RSP.USR.CONT.EML){
var valid = true;
console.log("Yes");
break;
}
}
if (valid) {
alert("Login was successful. Welcome, " + un + ".")
window.location = "https://www.google.com";
return false;
}
if (count >= 1) {
alert("The correct username is " + item_name + ", you put in "+un);
count--;
}
var responseData = {
authenticatUser: {
"ERR":0,
"RSP":{
"AUTHC":"true",
"USR":{
"ID":"2",
"TJT":"FULL",
"ACTV":"true",
"BO":"1489760664786",
"CONT":{
"FNM":"John",
"LNM":"Doe",
"PHN":"5556667777",
"PHNTP":"NONE",
"EML":"ex#mple.com",
"EMLTP":"NONE"
},
"ADMIN":"false",
"LLOGN":"1489760664786",
"ACCT":{
"ID":"2",
"TJT":"ID"
}
}
}
},
When you write:
if( un == account.responseData.authenticatUser.RSP.USR.CONT.EML){
var valid = true;
You are initializing a new valid variable that is only seen in that function. When you later access it outside the function you are seeing the original valid you initialized in line 5. This is called shadowing and it's a common source of bugs.
Do this instead:
if( un == account.responseData.authenticatUser.RSP.USR.CONT.EML){
valid = true;
Now you should be changing the original valid variable.

Get the value of Rich Text Editor in Umbraco backoffice

I've defined a property that is called Replyand the document type is RichTextEditor.
I cannot get the value of Reply. This problem is only for properties that type of it is RichTextEditor!!!
How can I get the value of Rich Text Editor in Umbraco backoffice?
I've used Umbraco 7.x and ASP.NET MVC.
angular.module("umbraco").controller("Reply.controller", function ($scope, $http, $routeParams) {
$scope.SendReply = function () {
var contentId = $routeParams.id;
var replyText = $("#Reply").val(); // without value ??? (type of Reply is RichTextEditor)
var email = $("#Email").val(); // It's OK.
var dataObj = {
ReplyText: replyText,
ContentId: contentId,
Email: email,
};
$http.post("backoffice/Reply/ReplyToIncomingCall/ReplyMessage", dataObj).then
(
function (response) {
alert("YES!");
//TODO:
}
);
}
});
For getting the value of Reply, you can use this code.
var replyList = $("[id*='Reply']");
for (i = 0; i < replyList.length; ++i) {
var rText = replyList[i].value;
if (!(rText === "" || rText === null)) {
replyText = rText;
}
}

Issue On Validating Text Field Using Custom Validation Function

Can you please take a look at This Demo and let me know what I am doing wrong in this Custom Function to validate the Text input?
$(function () {
var fname = $('#fname').val();
var lname = $('#lname').val();
var proceed = true;
function nameInput(inputData) {
var textBox = $.trim($(inputData).val())
if (textBox == "") {
alert('Field Can Not be Empty');
}
}
$("#pro").on("click", function (e) {
nameInput(fname);
e.preventDefault();
});
});
apparently the nameInput() is returning Field Can Not be Empty in both empty and filled format of the fname input. Thanks
You need to remove the two calls to val() when you declare your field variables:
var fname = $('#fname');
var lname = $('#lname');
As it is, you're passing the value to your method, and then in your method calling val() again.

Jquery can't pass variable through function

$(document).ready(function() {
//var audit_to_del;
//var type;
//var option_selected;
//var progress;
function redirect(audit_type) {
var page;
switch(audit_type){
case 'Simple 123':
page = 'smeg';
break;
}//end switch
return page;
}
$('#audit_summary_list_div').on("click", ".go_btn", function(e){
var audit_to_del = $(this).prev('.audit_to_del').val();
var type = $(this).closest('tr').find('.audit_type').text();
var option_selected = $(this).closest('td').prev().find('.option_select').val();
var progress = $(this).closest('tr').find('.progress').text();
var location = redirect(type);
alert(location);
});
});
If I pass a literal value through the function it works and returns 'smeg'
var location = redirect('Simple 123');
If I alert(type) the value is correctly shown as Simple 123
If I try to use
var location = redirect(type);
I get an undefined error
I have tried created global variables and then using them in the function
Your text has white-space in it making the condition false. Try that :
var location = redirect($.trim(type));

Categories

Resources