Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone,
I have tried to code the following in javascript not to allow numbers like 12121212/34343434.
Can anyone please check and let me know where I am going wrong




XML
<HTML>
 <HEAD>
  <TITLE> </TITLE>

 <script type="text/javascript">
  function formValidation(){
  if (validateAllSame()){
  }
  return false;
  }
  function validateAllSame(){

  var y=document.form1.number.value;
  var x = y.substr(0,2);
  for(int i=2;i<=y.length;i+=2){
  if(y.substr([i],2) != x){
  return true;
  }

  }

  return false;
  alert("wrong number");
  }

  </script>

 </HEAD>

 <BODY>
  <form name ="form1" action="" onsubmit="return formValidation()">
  Number:<input type="text" name="number" value="">
  <input type="submit" value="Done">
   </form>

 </BODY>
</HTML>
Posted
Comments
[no name] 10-Mar-13 14:15pm    
It would appear that you and http://www.codeproject.com/Questions/559638/Javascriptplusvalidationplusnotplustoplusallowplus are working on the same homework assignment. Maybe you could collaborate?
codegirl24 10-Mar-13 14:16pm    
yes.. we have come up with the code together but we both are unable to spot out where the problem could be.
[no name] 10-Mar-13 14:24pm    
And you think posting the same thing over and over is going to get a better answer? Did either of you debug this? Did they teach about bench checking in any of your classes? Did you bother trying to figure out what the actual output is?
codegirl24 10-Mar-13 14:31pm    
We are just learning and we dont mean posting questions just for fun.. we have tried out changing the code trying to arrive at what we are looking for.We assumed people here are helpful and just provide hints to improve the code.Moreover for people who are learning something new every little thing can be doubtful to attempt..If by just commenting on people's questions/making fun of them/trying to speak in a dominating way you are getting more points..please look for some other persons question.We are looking for someones help here and your comments are no help to us.
enhzflep 10-Mar-13 15:38pm    
If ThePhantomUpvoter's questions are of no use to you, you're probably better-off just skipping class for something you consider more interesting. They are basic questions for which you should have answers. If you can't infer what actions to perform in order to fix the code, address the points individually and ask further questions.

Since your question is rather loosely defined, I'll break it down how I see it.

1. We have to input a number from the user
2. We have to check the number to ensure that it meets the following criteria
(a) The number has an even number of digits, the number comprises a repetition of the first two digitis
(b) The number must not contain sequences of (N,N+1)
3. How would I achieve 2a or 2b (You decide which is the accurate definition of a prohibited number - you've not defined what it is about the numbers that may be used to test for them)

If your definition fits (a), then it's easy -
(1) Convert number to a string
(2) Check if string has an even number of digits
(3) get first two digits into a new string
(4) extract next pair of digits
(5) compare to result obtained in (3)
(6) if (reached end of string == 0) && (result #3 == result #4) goto 4

1 solution

Hi all,
I found the solution using a regular expression. Hope it would be helpful to anyone facing the same problem.Following is my function code...rest code is same as above.

C#
function validateAllSame(){
  var newregex=/([0-9]+?)\1+/;
  var y=document.form1.number.value;
  
  if((!(newregex.exec(y))) ){
  return true;
  }
  alert("wrong number");
  return false;
  }
 
Share this answer
 
Comments
Maciej Los 10-Mar-13 16:07pm    
Bravo codegirl24!
+5!
Marco Bertschi 10-Mar-13 17:00pm    
Good and useable solution. Have +5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900