Javascript text alert - javascript

How can I use JavaScript to recognize if a text box is filled, and if not, to alert me that it must be filled.
I have tried using
function myFunction() {
alert

You can try with the following fiddle, but its better if you google your query because it is just a basic query:
$("button").click(function(){
var inputVal = $("input[name=input]").val();
if(inputVal == "" || inputVal == null){
alert("Please fill the box");
}else{
alert("Good to go");
}
});
https://jsfiddle.net/thisisdg/s7bovqvw/

Related

interactive PDF Form-Validation Javascript

I'm trying to validate my interactive PDF. So if i click on a button (for validating) there's following code behind it:
var isBlank = false;
var blank = "Please fill following fields:";
var isNotValid = false;
var notValid = "Please check input data in following fields:";
var message = "";
var t = ['Division', 'Oragnisationseinheit', 'Name', 'KZZ', 'Privataddresse'];
var i;
for(var i=0; i<t.length;i++){
//validation text fields needs to be filled in
if (this.getField(t[i]).value == "") {
blank = blank + "\n" + this.getField(t[i]).name;
isBlank = true;
}
//validation text field must contain only lower case letters
if (/^[a-z]*$/.test(this.getField(t[i]).value) == false) {
notValid = notValid + "\n" + this.getField(t[i]).name;
isNotValid = true;
}
//generate message
if (isBlank == true) {
message = blank + "\n" + "\n";
}
if (isNotValid == true) {
message = message + notValid;
}
}
//check all conditions
if ((isBlank == true) || (isNotValid == true)) {
//show message
app.alert({ cMsg: message, cTitle: "Input data error" });
}
The problem is now, if I press the button there's no reaction. --> the var message wont being displayed. Where is the issue?
Thanks for all ideas.
You might try instead to add a custom validation script that would first check to be sure the field isn't blank and if not, simply change the input to lower case so the user doesn't need to modify the field themselves.
Add the following code to the custom field validate script. This should work for any text field.
if (event.value.length == 0) {
app.alert({ cMsg: event.target.name + " cannot be blank.", cTitle: "Input data error" });
}
else {
event.value = event.value.toLowerCase();
}

JavaScript validate various form elements

I have a script thats trying to validate some form elements but i cant figure out the correct syntax for each "if" I need, its doing absolutely nothing. I believe the issue may be that on the same form there are many "tabs" or other forms so I may have to write which form the element belongs to but I dont know the syntax for that, like if its
var NewStatus = document.getElementById("ChangeStatusForm.CURRENT_STATUS").VALUE;
if none of the alerts apply then i just need it to submit the form
<script type="text/javascript">
function ValidateInfo()
var NewStatus = document.getElementById("CURRENT_STATUS").VALUE;
var OldStatus = document.getElementByID("ovpInfo.current_status").VALUE;
var CheckNumber = document.getElementByID("check_number").VALUE;
var CheckDate = document.getElementByID("check_date").VALUE;
if (NewStatus == 0){
alert ("Please select a New Status");
}
else if (NewStatus == 5){
alert ("This claim has already been collected");
} else if (NewStatus == 10){
alert ("This claim has already been collected");
} else if (NewStatus == 10 and CheckNumber =="" or CheckDate == ""){
alert ("Please enter Payment Details");
else
document.forms['ChangeStatusForm'].submit() ;
}
</script>
In order to just get at least one variable check working ive edited my script. this does not work either though (used some syntax I found to specify the actual form the element belongs to)
<script type="text/javascript">
function ValidateInfo()
{
var NewStatus = document.forms["ChangeStatusForm"]["CURRENT_STATUS"].value;
if (NewStatus == 0){
alert ("Please select a New Status");
}
}
</script>
you are using a wrong expression for "document.getElementById("id")" and you need to change the or, and for "&&", "||".
And I using a flag for return the correct value.
for example:
function ValidateInfo()
var flag = true;
var NewStatus = document.getElementById("CURRENT_STATUS").value;
var OldStatus = document.getElementByID("ovpInfo.current_status").value;
var CheckNumber = document.getElementByID("check_number").value;
var CheckDate = document.getElementByID("check_date").value;
if (NewStatus == 0){
alert ("Please select a New Status");
flag=false;
}
else if (NewStatus == 5){
alert ("This claim has already been collected");
flag=false;
} else if (NewStatus == 10){
alert ("This claim has already been collected");
flag=false;
} else if (NewStatus == 10 and CheckNumber =="" or CheckDate == ""){
alert ("Please enter Payment Details");
flag=false;
}else{
flag=true;
}
if( flag ){document.forms['ChangeStatusForm'].submit()}
good luck

How to update text inside Div?

Iam trying to update Div text based on js variable null or not.
This is the code used for checking var userName is null or not.
If its null i dont want to do anything.But if userName is not null then i need to append ',' to existing text inside div .
$("document").ready(function (){
var userName = "";
if (userName == undefined || userName == null) {
alert('empty');
} else {
alert('not empty');
var div = document.getElementById('title b');
div.innerHTML = div.innerHTML + ',';
}
});
my html of the div
<div id="title" class="box">
<b>Welcome</b>
</div>
For example if userName is not null then i want div text as "Welcome ,"
if userName is null then i want div text as "Welcome"
Since you're using jQuery already...
$(document).ready(function (){
var userName = ""; // this will never get into the following if statement, always else
if (userName == undefined || userName == null) { // use if (!userName) as shorthand.
alert('empty');
} else {
alert('not empty');
var div = $('#title > b');
div.text(div.text() + ',');
}
});
You're using an odd mix of POJS and jQuery here. Here's a purely jQuery solution:
$("document").ready(function (){
var userName = "";
if (userName == undefined || userName == null) {
alert('empty');
} else {
alert('not empty');
$('#title b').append(',');
}
});
It's a little odd that you're declaring userName as an empty string within your function - the first if condition will never execute.
A more jQuery answer, if you are interested:
var userName = "";
//var userName = "User"; // for testing
//var userName; // for testing
//var userName = null; // for testing
$(document).ready(function (){
var greeting = !!userName ? "Welcome, "+ userName + "!" : "Welcome";
$("#title b").html(greeting);
})
jsFiddle

prevent button form from submission Javascript

This form keeps on submitting even if it executes the return value, what is the problem with my code?
function formhash (form, password)
{
var pass1 = document.getElementById("password").value;
var pass2 = document.getElementById("cpassword").value;
var ok = true;
if (password != cpassword) {
//alert("Passwords Do not match");
document.getElementById("password").style.borderColor = "#E34234";
document.getElementById("cpassword").style.borderColor = "#E34234";
ok = false;
return;
}
else
{
$.post('insert_home.php'
{PRIMAID:PRIMAID,EDITCAP:EDITCAP,EDITIMG:EDITIMG,EB_TITLE:EB_TITLE}).done(function(data){
alert ("Book Successfully Updated");
location.reload();
});
var p = document.createElement("input");
form.appendChild(p);
p.name="p";
p.type="hidden";
p.value=hex_sha512(password.value);
password.value="";
form.submit();
}
}
You are calling form.submit();, remove it and it won't submit.
You are using the wrong variable names "password" and cpassword". You created pass1 and pass2 so you need to use those.
Change to this:
//You were using the WRONG variable names
if (pass1 != pass2) {
//alert("Passwords Do not match");
document.getElementById("password").style.borderColor = "#E34234";
document.getElementById("cpassword").style.borderColor = "#E34234";
ok = false;
return false;
}
I don't see where formhash() is used.
The code calls the submit, not the function.
If it submits the form, it' because it's falling in the else condition.
Add lots of "console.log("debug_X")" where "X" is a number different each time.
You have to add inside a console.log the value of pass1 and pass2.
Maybe you are passign a value instead of an object or maybe the reversed situation can happen also.
My instinct tells me to check both password object, and their values.
Also it tells me that you didn't check the content of pass1 and pass2 before posting a comment to Don Rhummy.
Check carefully and please search more before posting your next answer.
You can have the value by doing "console.log(pass1);" and by opening firebug and by checking the javascript console.
Remove Action attribute from Form.

JavaScript redirection error

I am trying to use JavaScript to redirect to a PHP page on my site, however, when I do it, nothing happens, apart from the alert boxes if I do not fill in the parameters. Is there something I am doing wrong?
document.getElementById("submit").onclick = check;
function check()
{
if(document.getElementById("name").value == "")
alert("The field 'Name' is required.");
else if (document.getElementById("message").value == "")
alert("The field 'Message' is required");
else
window.location.href = "scripts/main/contact.php?msg=" + document.getElementById("message").value;
}
Your default form action takes place overriding your redirect. Return false from the handler to prevent it from taking place:
function check()
{
if(document.getElementById("name").value == "")
alert("The field 'Name' is required.");
else if (document.getElementById("message").value == "")
alert("The field 'Message' is required");
else
window.location.href = "scripts/main/contact.php?msg=" + document.getElementById("message").value;
return false; // <------ here
}

Categories

Resources