combo box validation - html

content this is the java script code.for validating combo boxes..the validation is not workin properly..i needed ur help..plz help me..if the user doesnt select it has to show an alert box for all the tree
<script type="text/javascript">
function checkseats() {
if (!document.getElementsByName("seats")[0].selectedIndex == 1) {
alert("Select No of Seats");
return false;
}
return true;
}
function checkmovie() {
if (document.getElementsByName("movie")[0].selectedIndex == 2) {
alert("Please select Movie from the list");
return false;
}
return true;
}
function checkdate() {
if (document.getElementsByName("date")[0].selectedIndex == 3) {
alert("Please select Date from the list");
return false;
}
return true;
}
function validate() {
checkdate();
checkseats();
checkmovie();
}
</script>

I think you want this instead,
if (document.forms["yourFormName"].seats.value == "0"){
alert("Select No of Seats");
return false;
}
return true;
}

Related

using condition in jquery onclick button

Im trying to confirm if the password strength is strong or weak and is the password is strong and when I submit it should have alert message like "You Have Strong Password" and when its weak "Invalid Password"
This is what I am now.
function checkPasswordStrength() {
var passwordStrength = false;
var number = /([0-9])/;
var alphabets = /([a-zA-Z])/;
var special_characters = /([~,!,#,#,$,%,^,&,*,-,_,+,=,?,>,<])/;
if ($('#password').val().length < 8) {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('weak-password');
$('#password-strength-status').html("Weak (should be atleast 8 characters.)");
} else {
if ($('#password').val().match(number) && $('#password').val().match(alphabets) && $('#password').val().match(special_characters)) {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('strong-password');
$('#password-strength-status').html("Strong");
return passwordStrength = true;
} else {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('medium-password');
$('#password-strength-status').html("Medium (should include alphabets, numbers and special characters.)");
}
}
}
$('#btn-submit').click(function () {
if (passwordStrength == false) {
alert("INVALID PASSWORD");
} else {
alert("You have Strong PASSWORD");
}
</script>
its for Educational Purpose only im just starting jquery..
thank you in advance..
You need to call the function instead of just checking your variable. So rather do
$('#btn-submit').click(function () {
if (checkPasswordStrength() === false) {
instead of
$('#btn-submit').click(function () {
if (passwordStrength == false) {
Then, instead of return passwordStrength = true; you should do just passwordStrength = true and add a return passwordStrength to the very end of your function so it will return either false or true.
It looks like the variable scope is incorrect. var passwordStrength should be put outside of the checkPasswordStrength function.
var passwordStrength
function checkPasswordStrength() {
....

Form validation for username and Email in java script

I'm trying to validate the value in my name element and email address element.
but the below code is not providing the proper validation and my code is still jumping to my php file with wrongs inputs in it.
can anybody please provide me some guidance on this?
var letters = /^[A-Za-z]+$/;
var mailformat = ^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$;
if( document.addform.First_name.value.match(letters)) {
return true;
}
else {
alert( "Username must have alphabet characters only" );
document.addform.First_name.focus() ;
return false;
}
if( document.addform.email.value.match(mailformat) ) {
return true;
}
else {
alert( "You have entered an invalid email address!" );
document.addform.email.focus() ;
return false;
}
You need to return true if both conditions are met, not just one of them.
function validateFormInput () {
let yourUsernameRegEx = new RegExp(...);
let yourMailRegEx = new RegExp(...);
if (!document.addform.First_name.value.match(yourUsernameRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
if (!document.addform.email.value.match(yourMailRegEx)) {
// Other code here (alerts, popups, etc.) ...
return false;
}
return true;
}

Parameter passing for multiple text fields that are gathered in an array?

So, I have a question. I'm currently working an an education program that teaches basics on cell organelles. I have lines pointing to each organelle, in which I want to have the user be able to input what each organelle name is (like a diagram). When the user has completed the work properly, I will display the next button.
However, in order to give the user the ability to proceed, I need a way of tracking whether or not a student's answers are correct. I am using parameter passing for this.
I would like to be able to return either true/false. If all answers are returned true, the user may advance. If even one answer is wrong, a message displays.
How do I use parameter passing to determine if a users answers are right/wrong? The textfields are in an array as well...
Here is the code.
Thanks!
-Zero;
import flash.events.MouseEvent;
var organelleInput:Array=[F18nucleolusInput_txt, F18nucleusInput_txt, F18erInput_txt, F18golgiInput_txt, F18vacuoleInput_txt, F18chloroplastInput_txt, F18lysosomeInput_txt, F18mitochondriaInput_txt];
F18next_btn.visible=false;
F18next_btn.addEventListener(MouseEvent.CLICK, F18goToFrameNineteen);
F18back_btn.addEventListener(MouseEvent.CLICK, F18goToFrameSix);
checkMyWork_btn.addEventListener(MouseEvent.CLICK, checkAnswers);
function checkAnswers(event:MouseEvent):void
{
var answer:String;
var correctAnswers:Boolean;
var incorrectAnswers:Boolean;
answer = organelleInput[i];
correctAnswers=checkCorrectAnswers(answer);
for(var j:int=0; j<organelleInput.length; j++)
{
organelleInput[j].restrict="a-z";
if(correctAnswers==true)
{
F18output_txt.text="Well done!";
F18next_btn.visible=true;
checkMyWork_btn.visible==false;
}
if(correctAnswers==false)
{
F18output_txt.text="One of them seems to be wrong. Try again.";
}
}
}
function F18goToFrameNineteen(event:MouseEvent):void{
gotoAndStop(19);
}
function F18goToFrameSix(event:MouseEvent):void{
gotoAndStop(6);
}
function checkCorrectAnswers(s:String):Boolean
{
if(F18nucleolusInput_txt.text=="nucleolus"){
return true;
}
return false;
if(F18nucleusInput_txt.text=="nucleus"){
return true;
}
return false;
if((F18erInput_txt.text=="endoplasmic reticulum")||(F18erInput_txt.text=="er")){
return true;
}
return false;
if((F18golgiInput_txt.text=="golgi body")||(F18golgiInput_txt.text=="golgi apparatus")){
return true;
}
return false;
if((F18mitochondriaInput_txt.text=="mitochondria")||(F18mitochondriaInput_txt.text=="mitochondrion")){
return true;
}
return false;
if((F18lysosomeInput_txt.text=="lysosome")||(F18lysosomeInput_txt.text=="lysosomes")){
return true;
}
return false;
if(F18vacuoleInput_txt.text=="vacuole"){
return true;
}
return false;
if((F18chloroplastInput_txt.text=="chloroplast")||(F18chloroplastInput_txt.text=="chloroplasts")){
return true;
}
return false;
}
You can check Array of TextFields vs Array of Arrays:
var Correct:Array = [
["nucleolus"],
["nucleus"],
["golgi body", "golgi apparatus"],
["mitochondria", "mitochondrion"]
];
var Answers:Array = [T1, T2, T3, T4];
// Returns true if all answers are correct.
function allCorrect():Boolean
{
for (var i:int = 0; i < Answers.length; i++)
if (!oneCorrect(Answers[i], Correct[i]))
return false;
return true;
}
// Returns true if answer is on the Array of correct answers.
function oneCorrect(source:TextField, target:Array):Boolean
{
return target.indexOf(source.text.toLowerCase()) > -1;
}

Flash AS3:Multiple function in one key?

Etc: if I press "space" key, inventory opens.But if I press "space" again, inventory should close.What should I use for this kind of thing?I can open it, but cant close.Thanks, sorry for bad english.(P.S: Im using flashdevelop+flixel)
Yes, this is completely possible.
var isInventOpen:Boolean = false;
function openCloseInvent(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.SPACE && isInventOpen == false)
{
//Open Inventory
isInventOpen = true;
}
if(e.keyCode == Keyboard.SPACE && isInventOpen == true)
{
//Close Inventory
isInventOpen = false;
}
}
More info on KeyCodes can be found here: KeyCodes, if you need it.
You should give yourself a boolean, like so:
var menuOpen:Boolean = false;
function yourEventHandler(e:KeyboardEvent):void {
if (menuOpen) {
menuOpen = false;
//close your menu here
} else {
menuOpen = true;
//open menu here
}
}

firebug saying not a function

<script type = "text/javascript">
var First_Array = new Array();
function reset_Form2() {document.extraInfo.reset();}
function showList1() {document.getElementById("favSports").style.visibility="visible";}
function showList2() {document.getElementById("favSubjects").style.visibility="visible";}
function hideProceed() {document.getElementById('proceed').style.visibility='hidden';}
function proceedToSecond ()
{
document.getElementById("div1").style.visibility="hidden";
document.getElementById("div2").style.visibility="visible";
document.getElementById("favSports").style.visibility="hidden";
document.getElementById("favSubjects").style.visibility="hidden";
}
function backToFirst () {
document.getElementById("div1").style.visibility="visible";
document.getElementById("div2").style.visibility="hidden";
document.getElementById("favSports").style.visibility="visible";
document.getElementById("favSubjects").style.visibility="visible";
}
function reset_Form(){
document.personalInfo.reset();
document.getElementById("favSports").style.visibility="hidden";
document.getElementById("favSubjects").style.visibility="hidden";
}
function isValidName(firstStr) {
var firstPat = /^([a-zA-Z]+)$/;
var matchArray = firstStr.match(firstPat);
if (matchArray == null) {
alert("That's a weird name, try again");
return false;
}
return true;
}
function isValidZip(zipStr) {
var zipPat =/[0-9]{5}/;
var matchArray = zipStr.match(zipPat);
if(matchArray == null) {
alert("Zip is not in valid format");
return false;
}
return true;
}
function isValidApt(aptStr) {
var aptPat = /[\d]/;
var matchArray = aptStr.match(aptPat);
if(matchArray == null) {
if (aptStr=="") {
return true;
}
alert("Apt is not proper format");
return false;
}
return true;
}
function isValidDate(dateStr) {
//requires 4 digit year:
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
alert("Date is not in a valid format.");
return false;
}
return true;
}
function checkRadioFirst() {
var rb = document.personalInfo.salutation;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please specify a salutation");
return false;
}
function checkCheckFirst() {
var rb = document.personalInfo.operatingSystems;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please specify an operating system") ;
return false;
}
function checkSelectFirst() {
if ( document.personalInfo.sports.selectedIndex == -1)
{
alert ( "Please select a sport" );
return false;
}
return true;
}
function checkRadioSecond() {
var rb = document.extraInfo.referral;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please select form of referral");
return false;
}
function checkCheckSecond() {
var rb = document.extraInfo.officeSupplies;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
return true;
}
}
alert("Please select an office supply option");
return false;
}
function checkSelectSecond() {
if ( document.extraInfo.colorPick.selectedIndex == 0 ) {
alert ( "Please select a favorite color" );
return false;
}
return true;
}
function check_Form(){
var retvalue = isValidDate(document.personalInfo.date.value);
if(retvalue) {
retvalue = isValidZip(document.personalInfo.zipCode.value);
if(retvalue) {
retvalue = isValidName(document.personalInfo.nameFirst.value);
if(retvalue) {
retvalue = checkRadioFirst();
if(retvalue) {
retvalue = checkCheckFirst();
if(retvalue) {
retvalue = checkSelectFirst();
if(retvalue) {
retvalue = isValidApt(document.personalInfo.aptNum.value);
if(retvalue){
document.getElementById('proceed').style.visibility='visible';
var rb = document.personalInfo.salutation;
for(var i=0;i<rb.length;i++) {
if(rb[i].checked) {
var salForm = rb[i].value;
}
}
var SportsOptions = "";
for(var j=0;j<document.personalInfo.sports.length;j++){
if ( document.personalInfo.sports.options[j].selected){
SportsOptions += document.personalInfo.sports.options[j].value + " ";
}
}
var SubjectsOptions= "";
for(var k=0;k<document.personalInfo.subjects.length;k++){
if ( document.personalInfo.subjects.options[k].selected){
SubjectsOptions += document.personalInfo.subjects.options[k].value + " ";
}
}
var osBox = document.personalInfo.operatingSystems;
var OSOptions = "";
for(var y=0;y<osBox.length;y++) {
if(osBox[y].checked) {
OSOptions += osBox[y].value + " ";
}
}
First_Array[0] = salForm;
First_Array[1] = document.personalInfo.nameFirst.value;
First_Array[2] = document.personalInfo.nameMiddle.value;
First_Array[3] = document.personalInfo.nameLast.value;
First_Array[4] = document.personalInfo.address.value;
First_Array[5] = document.personalInfo.aptNum.value;
First_Array[6] = document.personalInfo.city.value;
for(var l=0; l<document.personalInfo.state.length; l++) {
if (document.personalInfo.state.options[l].selected) {
First_Array[7] = document.personalInfo.state[l].value;
}
}
First_Array[8] = document.personalInfo.zipCode.value;
First_Array[9] = document.personalInfo.date.value;
First_Array[10] = document.personalInfo.phone.value;
First_Array[11] = SportsOptions;
First_Array[12] = SubjectsOptions;
First_Array[13] = OSOptions;
alert("Everything looks good.");
document.getElementById('validityButton').style.visibility='hidden';
}
}
}
}
}
}
}
}
/*function formAction2() {
var retvalue;
retvalue = checkRadioSecond();
if(!retvalue) {
return retvalue;
}
retvalue = checkCheckSecond();
if(!retvalue) {
return retvalue;
}
return checkSelectSecond() ;
} */
</script>
This is just a sample of the code, there are alot more functions, but I thought the error might be related to surrounding code. I have absolutely no idea why, as I know all the surrounding functions execute, and First_Array is populated.
However when I click the Proceed to Second button, the onclick attribute does not execute because Firebug says proceedToSecond is not a function
button code:
<input type="button" id="proceed" name="proceedToSecond" onclick="proceedToSecond();" value="Proceed to second form">
I had the same problem, and it's because you have a form with the same name as your function. JavaScript doesn't seem to be able to distinguish between the two, so you get the "not a function" error.
Maybe there is an error in your Javascript before that snippet given by you. If so, the rest will not be parsed by Firefox and then your function will not be defined.
The problem was resolved by changing proceedToSecond() to doIt() in both the call and the function name. I have no idea why
Hmm... I always used javascript:function_name() instead of function_name() because a few times the javascript didn't run. The name tag for the html snippet might have to be changed to a slightly different name because javascript might be getting it mixed up. Can you show us the entire javascript file because there may be a mistake/syntax error somewhere at the bottom/top.
It works on my computer, Firefox 3.5.5, Firebug 1.4.3, when inserting your code into an empty html document (<html><head/><body> code </body></html>)
Maybe there is another bug somewhere in your DOM, or in some of your other functions?
Could you possibly paste the entire source here, or maybe on a pastebin site?